This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've got the following in my code and I cant get to work the latter part of the code work. To be more precise on my question, this works the way I want up until I click OneClick. When that's done I get another form (form2) with a TwoClick submit button dynamically populated. The issue comes here. When I click the button TwoClick the whole 'form2' disappears. can some one throw me a clue as to where I have gone wrong?
Thanks.
<?php session_start(); ?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
?>
</div>
</body>
</html>
try this
<?php
session_start();
function putForm2(){
$myForm = "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
return $myForm;
}
?><!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo putForm2();
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{
echo putForm2();
echo "Text box is empty..."; return;
}
}
?>
</div>
</body>
</html>
Session_start() must be called before outputting any output (in the beggining of the script).
Sendtwo form disappears because of your logic - when you submit Sendtwo, $_POST['sendone'] is not set, therefore not being echoed. To fix that you could for example change the first condition to:
if (isset($_POST['sendone']) || isset($_POST['sendtwo']))
try putting
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
at the very end. i mean after the last }
Try this example and read the code comments. Though really its unclear to me what your trying todo so ive just ported code to what I think your trying todo, tho there are better ways handle processing forms. Also You should really think about your form value keys, txt1 and txt123 are not helpful and dont explain the type of variable you want from the form. Perhaps its of interest.
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
$form = null;
//Handle form 1
if(isset($_POST['sendone'])){
//reset session vars if new request
unset($_SESSION['txt1']);
unset($_SESSION['txt123']);
//validate txt1
if(!empty($_POST['txt1'])){
//set into session
$_SESSION['txt1'] = $_POST['txt1'];
//or you could put the value in a <input type="hidden" name="txt1" value="'.htmlspecialchars($_POST['txt1']).'"/>
$form = "
<form name='form2' method='post' action=''>
<table border='1'>
<tr>
<td>form 2 is here!<br></td>
</tr>
<tr>
<td><input type='text' name='txt123' id='txt123'></td>
</tr>
<tr>
<td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td>
</tr>
</table>
</form>";
} else {
$error['txt1'] = 'txt1 is empty!';
}
}
//do second form
if(isset($_POST['sendtwo'])){
//validate
if(!empty($_POST['txt123'])){
//set session
$_SESSION['txt123'] = $_POST['txt123'];
}else{
$error['txt123'] = 'txt2 is empty!';
}
}
//check then do something with both form values
if(empty($error) && isset($_SESSION['txt1']) && isset($_SESSION['txt123'])){
$form = "Both txt1=".htmlspecialchars($_SESSION['txt1'])." and txt123=".htmlspecialchars($_SESSION['txt123'])." was set into session";
}
}
?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="">
<input type='text' name='txt1' id='txt1'>
<input type="submit" name="sendone" id="sendone" value="OneClick">
<?php echo isset($error['txt1'])?$error['txt1']:null;?>
</form>
</div>
<div id="two">
<?php echo isset($form)?$form:null;?>
</div>
</body>
</html>
Related
I can't figure this out alone. I have .html file and in body section I'm importing some .php files using switch. I'm displaying table and I'm navigating on it with POST form. Now I want to add to my navigation bar search input which also will send data by POST. It is located on head tag and when I press the button nothing really happens.
index.php
<html>
<head>
<div class="leftNav">
<a class="buttonNav" href="index.php?subpage=table&data=session&page=0">Session</a>
<a class="buttonNav" href="index.php?subpage=table&data=all&page=0">All</a>
</div>
<div class="rightNav">
<?php include 'navbars.php' ?>
</div>
</head>
<body style="font-family:Verdana;">
<div class="content">
<?php
if(isset($_GET['subpage']))
{
switch($_GET['subpage'])
{
case 'table':
include 'datatable.php';
break;
default:
include 'home.php';
break;
}
}
else include 'home.php';
?>
</div>
</body>
</html>
datatable.php
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['search'])
{
$filtr = $_POST['filtr'];
$data = $_GET['data'];
header("
Location: index.php?subpage=table&data=".$data."&page=0&search=".$filtr);
}
else{...}
}
//DISPLAYING TABLE//
echo("
</table>
<form class='nav' method='POST'>");
$disabled = '';
if($curr+1 == 1)
$disabled = "disabled='disabled'";
echo("<input type='submit' name='f' value='First page' ".$disabled.">
<input type='submit' name='p' value='Previous page' ".$disabled."> ");
$disabled = "";
if($curr == $max)
$disabled = "disabled='disabled'";
echo("<input type='submit' name='n' value='Next page' ".$disabled.">
<input type='submit' name='l' value='Last page' ".$disabled."></form>");
?>
navbars.php
<?php
if(isset($_GET['subpage']) && $_GET['subpage'] == 'table')
{
echo("
<form method='POST'>
<input type='text' name='filtr'>
<input type='submit' name='search' value='Search'>
</form>
");
}
?>
As noted in the other answer, you will want to use the action attribute but you'll want to pass the $_GET['subpage'] as in:
action="<?php if(!empty($_GET['subpage'])) echo '?subpage='.htmlspecialchars($_GET['subpage'], ENT_QUOTES) ?>"
Note: I don't see anywhere that you actually send the $_GET['subpage'] or $_GET['data'] in your scripts in the first place, so I don't know if that is even being called properly.
Try Adding form action if you are referring to same page,
action="<?php echo $_SERVER["PHP_SELF"];?>"
hope it helps you.
So I'm currently working on a little project that allows a user to input a number into a textbox, after clicking a button that says "add" it should store that value into an array and then allow the user to input another value into that array. There is also a button on the page when the user is finished and wants to sum the values called "Submit". The problem I'm running into is everytime the form posts back, it recreates a new blank array. Any tips?
See the code below:
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
</p>
<?php
$array = array();
if (isset($_POST['submit']))
$num = $_POST['strNumber'];
$array[] = $num;
foreach($array as $num)
echo $num . ' + ';
if(isset($_POST['calculate']))
foreach($array as $num)
echo $num . ' + ';
?>
</form>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
<input type='submit' name='clear' value='clear' />
</p>
<?php
if (isset($_POST['submit'])) {
if(!array_key_exists("numbers", $_SESSION)) {
$_SESSION["numbers"] = array();
}
array_push($_SESSION["numbers"], $_POST["strNumber"]);
}
if(isset($_POST['clear'])) {
$_SESSION["numbers"] = array();
}
if(array_key_exists("numbers", $_SESSION)) {
echo implode("+", $_SESSION["numbers"]);
}
if(isset($_POST['calculate'])) {
if(array_key_exists("numbers", $_SESSION)) {
$expression = implode("+", $_SESSION["numbers"]);
eval( '$result = (' . $expression . ');' );
echo "=" . $result;
}
}
?>
</form>
</body>
</html>
Start a session
When the action is "submit"
Check if the session which will store the numbers is initialized
If it's not initialize it as an array
Finally push the number into the array
Check if there is a session initialized if there is print all the numbers ( you can use implode to do that)
if the action is calculate .. just make the calculation ( check eval function )
Hi I have a question about generating rows and columns. would like to ask about how can I make it in one page only. This is what I have tried.
HTML:
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="get_table/execute_table.php" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
PHP:
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Yes, it is fully running but I want it in 1 page only. Thanks.
Normally, if you want it on the same page, you just omit the action="" with its value.
Then of course, put the php process in the same page as the form:
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<!-- ^^ no more value, well you could just put the same filename -->
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
<?php
$out = ''; // initialize a string holder and when the submission is done, concatenate all the strings
if(isset($_POST['submit'])) { // catch submission button
$row = $_POST['title1'];
$column = $_POST['title2'];
$out .= "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
$out .= "<tr>";
for($td=1;$td<=$column;$td++){
$out .= "<td>row: ".$tr." column: ".$td."</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
}
echo $out; // finally echo it
to achieve this by using only one page just leave the action attribute empty. and add your php block on top.and make sure to save it as .php and place your php code block on top of all html
so in the end it will look like this
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
You need to post to the same page, and check if the PhP Post array has data yet.
Replace the form action with:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
Note: although most (all?) browsers support self-posts with a blank action, this is not technically W3C compliant.
Then when the page is submitted it will reload the same page and populate the POST array. Somewhere on your page add a condition similar to:
if($_POST['title']){
//do whatever get_table/execute_table.php did
}else{
//echo the form here or, if you're allowed, use an include()
}
More info on self-posting:
How do I make a PHP form that submits to self?
im creating a form which will display data from database, everything is set so far, but i want the form to remember the data in case if user has to write it down again in case of error. I found some similar code concerning only html, but when including php to display a form i find some difficulties for code to remember the last input (current problem is only concerning drop down selection list):
$Type = $_POST['petType']; //this should remember last input
<?php
/*upload form and drop-down selection list*/
echo "
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>\n
<option value='-1'>Type:</option>";
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
echo "<option value='$petType' ";
if($Type == '$petType')
{
echo "? selected='selected'";
// i need to make selected true only for last selected option,
//and redisplay it in the same form again
}
echo ">$petType </option>";
}
echo "</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
?>
try this:
<?php
$Type = $_POST['petType']; //this should remember last input
?>
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' <?php echo $Type == $petType ? "selected=selected":"";?> ><?php echo $petType;?> </option>
}
</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
sidenote: make html and php different , it makes things easier.
You are comparing $Type to a string not a variable.
Try
if($Type == $petType)
I'm trying to display a multiple page form that preserves submitted data from one page to the next using session tracking. $_POST['stage'] determines which form should be displayed. Each form has a hidden input type with a value set to increment the $stage variable by 1 but when I submit the data from the first form, the value of $stage seems to remain the same as I don't see the next form. Sessions is enabled in php.ini.
Here's my example:
<?php
session_start();
//Determine which integer to assign to the stage
if (($_SERVER['REQUEST_METHOD'] == 'GET') || (!isset($_POST['stage']))) {
$stage = 1;
} else {
$stage = (int) $_POST['stage'];
}
//Save any submitted data
if ($stage > 1) {
foreach ($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My Form Example</title>
</head>
<body>
<?php if ($stage == 1) { ?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
<label for='firstField'>First field:</label>
<input type='text' name='first_field /><br />
<input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
<input type='submit' value='Next' />
</form>
<?php } else if ($stage == 2) { ?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
<label for='secondField'>Second field:</label>
<input type='text' name='second_field /<br />
<input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
<input type='hidden' value='Done' />
</form>
<?php } ?>
</body>
</html>
Try adding session_start() at the top of your page. That's the first thing I've noticed.
I discovered a typo on my working script. Hindsight, I should have copied and pasted my entire script. Sorry. This script works fine (with session_start() at the beginning which still doesn't appear after I post the question).
Since you didn't mention what a problem is, which made me to sift and find exactly, I will put more precise answer. The problem is no ending single quote in the lines
<input type='text' name='first_field /><br />
and
<input type='text' name='second_field /<br />
and so my final working script is
<?php
session_start();
$stage = 0;
//Determine which integer to assign to the stage
if (!isset($_POST['stage'])) {
$stage = 1;
} else {
$stage = (int) $_POST['stage'];
}
//Save any submitted data
if ($stage > 1) {
foreach ($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My Form Example</title>
</head>
<body>
<?php if ($stage == 1) { ?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
<label for='firstField'>First field:</label>
<input type='text' name='first_field' /><br />
<input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
<input type='submit' value='Next' />
</form>
<?php } else if ($stage == 2) { ?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
<label for='secondField'>Second field:</label>
<input type='text' name='second_field' /<br />
<input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
<input type='hidden' value='Done' />
</form>
<?php } ?>
</body>
</html>