/    Sign up×
Community /Pin to ProfileBookmark

Form Select Help

Hello:

I have a form where there are two select boxes. One select box displays the loan programs available. The second select box displays the lenders.

When I submit the form and it ran correctly, I want to display a success statement something like “Rate updated for Program: <name> AND Lender: <name>.

I tested my script and the insert/update works perfectly. My problem is my success statement. It shows: Rate Updated for Program: $program AND Lender: $lender! The $program and $lender variable should show the name of the program and the name of the lender which was selected from the select box.

In the update query, you will see that the program_id and lender_id passed without a problem because the tables do get updated correctly. I don’t know why the name of both the program($program) and lender($lender) are not working.

Can someone help me out? The code is posted below.

Thank you in advance for all your help.

[code=php]
<?php

$page_title = ‘Rate Insert and Update’;

session_start();
$status = $_SESSION[“status”];
switch ($status)
{
case “Not logged”:
include “login.php”;
break;
case “”:
include “login.php”;
break;
case “Logged”:

include (‘header4.html’);

echo ‘<h2>Rate Update Screen</h2>’;

require_once (‘mysql_connect.php’);

if (isset($_POST[‘submitted’])) {

$errors = array();

//check for program.
if (!isset($_POST[‘program’])) {
$error[] = ‘You forgot to select a program.’;
} else {
$program = escape_data($_POST[‘program’]);
}

//check for lender.
if (!isset($_POST[‘lender’])) {
$error[] = ‘You forgot to select a program.’;
} else {
$lender = escape_data($_POST[‘lender’]);
}

//check for rate.
if (!is_numeric($_POST[‘rate1’])) {
$errors[] = ‘You forgot to enter a rate.’;
} else {
$rate1 = (float) $_POST[‘rate1’];
}

//check for price.
if (!is_numeric($_POST[‘price1’])) {
$errors[] = ‘You forgot to enter a price.’;
} else {
$price1 = (float) $_POST[‘price1’];
}

//check for rate.
if (!is_numeric($_POST[‘rate2’])) {
$errors[] = ‘You forgot to enter a rate.’;
} else {
$rate2 = (float) $_POST[‘rate2’];
}

//check for price.
if (!is_numeric($_POST[‘price2’])) {
$errors[] = ‘You forgot to enter a price.’;
} else {
$price2 = (float) $_POST[‘price2’];
}

//check for rate.
if (!is_numeric($_POST[‘rate3’])) {
$errors[] = ‘You forgot to enter a rate.’;
} else {
$rate3 = (float) $_POST[‘rate3’];
}

//check for price.
if (!is_numeric($_POST[‘price3’])) {
$errors[] = ‘You forgot to enter a price.’;
} else {
$price3 = (float) $_POST[‘price3’];
}

//check for rate.
if (!is_numeric($_POST[‘rate4’])) {
$errors[] = ‘You forgot to enter a rate.’;
} else {
$rate4 = (float) $_POST[‘rate4’];
}

//check for price.
if (!is_numeric($_POST[‘price4’])) {
$errors[] = ‘You forgot to enter a price.’;
} else {
$price4 = (float) $_POST[‘price4′];
}

if (empty($errors)) {

// Add new lender.
$query = “UPDATE test SET
program_id=’$_POST[program_id]’, lender_id=’$_POST[lender_id]’, rate1=’$rate1′, price1=’$price1′,
rate2=’$rate2′, price2=’$price2′, rate3=’$rate3′, price3=’$price3′, rate4=’$rate4′, price4=’$price4′
WHERE program_id=’$_POST[program_id]’ AND lender_id=’$_POST[lender_id]'”;
$result = mysql_query ($query) or die (mysql_error());

if (mysql_affected_rows() == 1) { // If it ran OK.

// Finish the page.
echo ‘<h3>Rate Updated for Program: $program AND Lender: $lender!</h3>’;
}

else $query = “INSERT INTO test (program_id, lender_id, rate1, price1, rate2, price2, rate3, price3,
rate4, price4)
VALUES (‘$_POST[program_id]’, ‘$_POST[lender_id]’, ‘$rate1’, ‘$price1’, ‘$rate2’, ‘$price2’,
‘$rate3’, ‘$price3’, ‘$rate4’, ‘$price4’)”;
$result = mysql_query ($query) or die (mysql_error());

if (mysql_affected_rows() == 1) { // If it ran OK.

// Finish the page.
echo ‘<h3>Rate Added!</h3>’;
}

}
else {
echo ‘<p><font color=”red”>Submission could not be completed. Please try again!</font></p>’;
}

}

?>
<form action=”admin_rate_update.php” method=”post”>
<table>

<tr><td>Loan Program</td>
<td><select name=”program_id”>
<option value=””>Please Select</option>
<?php
$sql = “SELECT * FROM loan_program”;
$resultset = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_assoc( $resultset ) ) {
$program_id = $row[‘id’];
$program = $row[‘program’];
print ‘<option value=”‘ . $program_id . “” >” . $program . “</option>n”;
}
?>
</select></td>
</tr>

<tr><td>Lender</td>
<td><select name=”lender_id”>
<option value=””>Please Select</option>
<?php
$sql = “SELECT * FROM lender”;
$resultset1 = mysql_query( $sql ) or die(mysql_error());
while( $row1 = mysql_fetch_assoc( $resultset1 ) ) {
$lender_id = $row1[‘id’];
$lender = $row1[‘lender’];
print ‘<option value=”‘ . $lender_id . “” >” . $lender . “</option>n”;
}
?>
</select></td>
</tr>
<tr><td></td></tr>
<tr><td>Rate</td><td>Price</td>
<tr><td> <input type=”text” name=”rate1″ size=”7″ maxlength=”10″ /></td>
<td><input type=”text” name=”price1″ size=”7″ maxlength=”10″/></td></tr>
<tr><td> <input type=”text” name=”rate2″ size=”7″ maxlength=”10″ /></td>
<td><input type=”text” name=”price2″ size=”7″ maxlength=”10″/></td></tr>
<tr><td> <input type=”text” name=”rate3″ size=”7″ maxlength=”10″ /></td>
<td><input type=”text” name=”price3″ size=”7″ maxlength=”10″/></td></tr>
<tr><td> <input type=”text” name=”rate4″ size=”7″ maxlength=”10″ /></td>
<td><input type=”text” name=”price4″ size=”7″ maxlength=”10″/></td></tr>

<tr><td><label for=”blank”>&nbsp;</label></td><td><input type=”submit” name=”btnSubmit” id=”btnSubmit” value=”Submit” class=”btn” />
<input type=”hidden” name=”submitted” value=”TRUE” /></td></tr>

</table>

</form>

<p><a href=”welcome.php”>Return to Main Menu</a></p>
<?php
include(‘footer3.html’);
?>
<?
}
?>
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@trepidityMar 04.2007 — 
<i>
</i>echo '&lt;h3&gt;Rate Updated for Program: $program AND Lender: $lender!&lt;/h3&gt;';

[/quote]


That's your problem. In PHP, when you enclose a string in single quote notation, it will be displayed literrally as what is inside the string except in the case of escape sequences. In other words, your variables' names aren't being expanded to their values but instead treated as part of a string literal. If you must use single quotes then you have to concatenate the variables with the string to get the values, but it is much easier to use double quotes instead in this case. So switch that to this:

<i>
</i>echo "&lt;h3&gt;Rate Updated for Program: $program AND Lender: $lender!&lt;/h3";


I found a link to a page that can explain this to you if you need it too.
Copy linkTweet thisAlerts:
@focus310authorMar 04.2007 — Hello:

I changed my single quotes to double quotes to read as such
[code=php]
echo "<h3>Rate Updated for Program: $program AND Lender: $lender!</h3>";
[/code]


I ran my script and received the following messages:

Notice: Undefined variable: program in C:InetpubfullfocusInformedadmin_rate_update.php on line 113

Notice: Undefined variable: lender in C:InetpubfullfocusInformedadmin_rate_update.php on line 113

Rate Updated for Program: AND Lender: !

Any ideas?
Copy linkTweet thisAlerts:
@trepidityMar 04.2007 — Yeah with a just a quick once over I think it's a scope issue. Define those two variables so they have scope over the entire script, before you set the values. I haven't looked through your code to check if there will be problems doing this, but you could just change the declarations you already have to global declarations like:

global $variable = somevalue;

The issue is doing that is that the variable is now a reference variable, not a value. This may or may not cause you problems, I would give it a shot first. Then if you find other errors, just predefine each of the two variables at the begining of the script. When you define a variable inside anything using brackets, such as a function. Scope for that variable is within that block. Does that make since?
Copy linkTweet thisAlerts:
@trepidityMar 04.2007 — Here I don't know what you background in programming is so...for example:
[code=php]
if(true){
$a = 1;
}

echo $a;
[/code]

That should throw an error. But this wouldn't:
[code=php]
$a = 0;
if(true){
$a =1;
}

if(true){
global $b = 2;
}

echo "a = $an";
echo "b = $b";
[/code]


It should output unless i'm just way off:

a = 1;

b = 2;
Copy linkTweet thisAlerts:
@Paul_JrMar 05.2007 — The problem is those variables are defined later in the script, are accessed at a different time than the part of the script from which you are calling them, and contain different values than you're trying to display. Simply replace them with $_POST['program'] and $_POST['lender'] (seeing as you've already done some error handling) and you should be good to go.

To expound a bit, when the page loads initially you query the database to build your form, and that is where you define $program, $program_id, $lender, and $lender_id. However, when the form is submitted, the code where you call those variables (where you're having the problem) is run before the code block in which they are defined. Also, if you were some how able to access them anyway, the query that grabs their values initially wouldn't have run again, which means you wouldn't get the info that was just updated by your UPDATE query (the data just submitted by the user), you'd get whatever is before that (whatever was submitted by the previous user).
×

Success!

Help @focus310 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...