Avatar of brendan-amex
brendan-amex
 asked on

Sending PHP data with form

I have a PHP page that pulls from a MySQL database and it displays everything perfectly. I now have added another field which is a form submit button that I have redirect to another page in order to echo the value I want. For some reason when the second php page loads it's blank. What am I doing wrong? Can PHP code be imbedded into an HTML form? My code is below.

And then here is my test.php page:

<?php
$select1 = $_POST['select1'];

echo $select1;
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
<th>Delete Row</th>
</tr>
</table>

<table border="1" cellspacing="2" cellpadding="2">

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Field1");
$f2=mysql_result($result,$i,"Field2");
$f3=mysql_result($result,$i,"Field3");

?>

<tr>
<td width="300"><font size="3" face="Calibri, Calibri, Calibri"><?php echo $f1; ?></font></td>
<td width="80"><font size="3" face="Calibri, Calibri, Calibri"><?php echo $f2; ?></font></td>
<td width="300"><font size="3" face="Calibri, Calibri, Calibri"><?php echo $f3; ?></font></td>

<td><form name="select1" form method="post" action="test.php">
<input type="hidden" id="select1" name="select1" value="<?php $f1; ?>" />
<input type="submit" name="submit" value="Submit" /></form></td>

</tr>

Open in new window

HTMLPHPWeb Development

Avatar of undefined
Last Comment
brendan-amex

8/22/2022 - Mon
Darude1234

The form tagged is double defined. You need to erase the second form tag
<form name="select1" form method="post" action="test.php">

Further the closing tag of you while-tag is missing, that should display an error on your screen.
Is this the whole script?
brendan-amex

ASKER
Sorry, I close the while loop here:

<?php
$i++;
}
?>

I took out the extra form, still doesn't work.
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
<th>Delete Row</th>
</tr>
</table>

<table border="1" cellspacing="2" cellpadding="2">

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Field1");
$f2=mysql_result($result,$i,"Field2");
$f3=mysql_result($result,$i,"Field3");

?>

<tr>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>

<td><form name="select1" method="post" action="test.php">
<input type="hidden" id="select1" name="select1" value="<?php $f1; ?>" />
<input type="submit" name="submit" value="Submit" /></form></td>

</tr> 

<?php
$i++;
}
?>

Open in new window

Dave Baldwin

Your 'test page' will only show something if you have POSTed a 'select1' value to it.  If you load it by itself, there is nothing to POST and so nothing will show.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
brendan-amex

ASKER
The test.php page posts select1 and puts it into $select1 variable, which is what I try to echo (see below). Will that not work? What will?

Can this value be stored into the hidden field? value="<?php $f1; ?>"

<?php
$select1 = $_POST['select1'];

echo $select1;
?>

Open in new window

Dave Baldwin

But unless you have a form on a page that will POST that value to that page, there is nothing there.  Here is a demo that does it.  Save it as 'post-test2.php' and run it on your PHP-enabled server.  It POSTs to itself.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Post Test2</title>
</head>
<body>
<h1>Post Test2</h1>

<?php
error_reporting(E_ALL);

if(isset($_POST['submit'])) {
	$test = $_POST['test'];
	echo "<b>The last value for 'test' was $test!</b>";
	}
?>

<p>Enter a value for test and click on submit:</p>

<form name="frm" action="post-test2.php" method="post">
Test: <input type="text" name="test" value="abc" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

<a href="post-test2.php">Start over.</a>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Sandeep Kothari

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
brendan-amex

ASKER
Thank you! That's what I was looking for.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.