Jump to content

Recommended Posts

Hi all,

I'm having difficulty getting the information to send properly from this form. Basically I want the "value" of the option to be equal to the ID number fetched from the database ($row['id']), however I can't seem to get it to send across to the next page.

 

This is the form to send the number:

 

// Get all the data from the dogs table
        $result = mysql_query("SELECT * FROM dogs
              WHERE owner=".$_SESSION['id']) or die(mysql_error()); 

echo '<bR><br>Select a dog for a basic checkup<br><form action="basiccheckup.php" method=post> 
<select name="DogID">';
             
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table

$dogid = $row['id']; 

echo '<option value=$dogid name=$dogid>'; 
echo $row['name'];
echo '</option>';

}
echo '</select>';

 

... and this is how I'm trying to collect the data on the next page

// fetch info from form
$dogname = $_POST['$dogid'];
echo $dogname;

 

I've never made a form where what's being sent is a PHP variable before, and can't seem to get it to work! I'd really appreciate it if anybody is able to help.

 

Please let me know if you need more details!

Thanks

Link to comment
https://forums.phpfreaks.com/topic/207303-having-issues-with-a-form/
Share on other sites

Thanks, but now it just doesn't send anything through again :[

 

I can't work out where the missing link is. When, on page one, I echo '$dogid' it works (well, it echoes the ID of the last one on the dropdown list), but then it doesn't work when I try to echo '$dogname' on the next page.

Cannot really help you without seeing your current code AND have you done a 'view source' of the form in your browser so that you know it is being correctly produced?

 

Edit: And posting your while code for your form would help in case it contains other problems.

Oof. Okay I still haven't solved this, so here's the full code for each of the pages (bar, of course, the connecting to the server bits)...

 

// Connect to server and select database.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");

echo "Welcome to the Vets (n.b. not yet working)<br>
Here the trained veterinarians will check your dogs over and give them certificates of clean health,<br> which will need to be renewed regularly in order to compete in training and races.";

// Make a MySQL Connection
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

// Get all the data from the dogs table
        $result = mysql_query("SELECT * FROM dogs
              WHERE owner=".$_SESSION['id']) or die(mysql_error()); 
              
              while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table

$dogid = $row['id']; }

echo '<bR><br>Select a dog for a basic checkup<br><form action="basiccheckup.php" method=post> 
<select name=\"$dogid\">';
             
// Get all the data from the dogs table
        $result = mysql_query("SELECT * FROM dogs
              WHERE owner=".$_SESSION['id']) or die(mysql_error());              
             
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table


echo '<option value=\"$dogid\">'; 
echo $row['name'];
echo '</option>';

}
echo '</select>';


// A simple example of a form.

echo '
<input type=submit name=submit value="Basic Health Check? $100">
</form>'; 

// Get all the data from the dogs table
        $result = mysql_query("SELECT * FROM dogs
              WHERE owner=".$_SESSION['id']) or die(mysql_error()); 

echo '<bR>Select a dog for a special pre-race checkup<br><form action=preracecheckup.php method=post> <select name=name>';
             
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table

echo '<option value="dog2">'; 
echo $row['name'];
echo '</option>';

}
echo '</select>';


// A simple example of a form.

echo '
<input type=submit name=submit value="Pre-race Health Check? $150">
</form>'; 


include('footer.php');

 

 

and then the second page...

// Connect to server and select database.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");

// fetch info from form
$dogname = $_POST['$dogid'];
echo $dogname;

// test to see if person has enough money to complete the checkup 
        $result = mysql_query("SELECT onhand FROM players
              WHERE id=".$_SESSION['id']) or die(mysql_error()); 

while($row = mysql_fetch_array( $result )) {
$testmoney = $row['onhand'];}

if(($testmoney - '100') < '0') { echo "Oops, you don't have enough money for this!"; }
else {

// set buyer as new owner

$sql2=("UPDATE dogs SET vetstatus = 'Vet Certificate Obtained' WHERE id='$dogname'");
$result2=mysql_query($sql2);

// subtract money from buyer

$sql3=mysql_query("UPDATE players SET onhand = onhand - '100' WHERE id=".$_SESSION['id']);
$result3=mysql_query($sql3);


// if successfully updated. 
if($result2){ echo "$dogname has been given a clean bill of health by the vet, who hands you a certificate to that effect.<br> You will now be able to enter this dog in training!";
echo " $100";
echo " has been deducted from your account in veterinary fees.";}
else{ echo "Oops, didn't work";}

}

include('footer.php');

 

Just did view source as you suggested and the form looks like its being reproduced fine... at least I think so. Ought it to be showing the value of the options as ""$dogid\"? I'm pretty sure it ought to be showing those as the IDs it has got from the database there?  :shrug:

 

Welcome to the Vets (n.b. not yet working)<br>
Here the trained veterinarians will check your dogs over and give them certificates of clean health,<br> which will need to be renewed regularly in order to compete in training and races.<bR><br>Select a dog for a basic checkup<br><form action="basiccheckup.php" method=post> 
<select name=\"$dogid\"><option value=\"$dogid\">My Favrit</option><option value=\"$dogid\">hg</option><option value=\"$dogid\">jhju</option><option value=\"$dogid\">Wanda</option><option value=\"$dogid\">Hippe</option><option value=\"$dogid\">o</option><option value=\"$dogid\">Carlos</option><option value=\"$dogid\">Violette</option></select>
<input type=submit name=submit value="Basic Health Check? $100">
</form><bR>Select a dog for a special pre-race checkup<br><form action=preracecheckup.php method=post> <select name=name><option value="dog2">My Favrit</option><option value="dog2">hg</option><option value="dog2">jhju</option><option value="dog2">Wanda</option><option value="dog2">Hippe</option><option value="dog2">o</option><option value="dog2">Carlos</option><option value="dog2">Violette</option></select>
<input type=submit name=submit value="Pre-race Health Check? $150">
</form><hr>

I had posted the line exactly as it should have been, but you changed the structure and quoting. That's why the literals are in the html source.

 

// get rid of:
echo '<option value="dog2">'; 
echo $row['name'];
echo '</option>';

replace it with:
echo "<option value=\"$dogid\">{$row['name']}</option>";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.