Using $_POST data to create a new html form value - php

I'm using post to send info to my page. I'm using that to create a variable and pass that variable to a new <input>. However, when I submit the new form, the variable is blank.
The variable is created from post data:
$timenotstarted = $_POST["placeholder"];
Then inline in the html:
<form name="newform" method="post" action="insert.php">
<input type="text" name="timerset" value="<?php echo $timenotstarted; ?>">
<input type="submit" value="Submit"><
</form>
When the new form is submitted, the variable whose name="timerset", is blank.
When I echo $timenotstarted on the same page that this form is,
it will show its value. It just "goes away" when I try to use it in the new form.
Edit: Here's what's happeneing:
Not sure if this helps: http://forexguruguide.com/timer/insert.php
And here's the whole shabang:
<?php include 'connect.php';
$timenotstarted = $_GET["placeholder"];
$start = $_POST["start"];
$timerset = $_post["timerset"];
echo $timenotstarted . '<br />';
echo $start . '<br />';
echo $timerset . '<br />';
?>
<form name="timeform" method="get" action="insert.php">
<select name="placeholder">
<option value="1">1 min</option>
<option value="3">3 min</option>
<option value="5">5 min</option>
<option value="10">10 min</option>
</select>
<input type="submit" value="Set timer">
</form>
<form name="startform" method="post" action="insert.php">
<input type="hidden" value="1" name="start">
Timer set to: <input type="text" name="timerset" value="<?php print $timenotstarted?>">
<input type="Submit" value="Start Timer">
</form>
<?php
if ($start==1) {
$target = time() + ($timerset * 60);
mysql_query("UPDATE countdowntimer SET target='$target' WHERE id='0'");
mysql_query("UPDATE countdowntimer SET timenotstarted='null' WHERE id='0'");
echo 'Timer started' . '<br />';
echo $target . '<br />';
echo time(); }
else if (!empty($timenotstarted)) {
$timenotstarted .= ":00";
mysql_query("UPDATE countdowntimer SET timenotstarted='$timenotstarted'");
echo 'Timer set to: ' . $timenotstarted; }
else {
echo 'Set the timer then start the timer'; }
?>

shouldn't use POST for that. It's GET's job
<? if (!isset($_GET['placeholder'])): ?>
<form>
Enter placeholder:
<input type="text" name="placeholder" value="">
<input type="submit" value="Submit">
</form>
<? else: ?>
<form name="newform" method="post" action="insert.php">
<input type="text" name="timerset" value="<?=htmlspecialchers($_GET["placeholder"])?>">
<input type="submit" value="Submit">
</form>
<? endif ?>

It's not clear to me what you're doing wrong from your post, but here's the basics on how to do it right:
page1.php
<form action="page2.php" method="post">
<input type="hidden" name="foo" value="bar"/>
<input type="submit"/>
</form>
page2.php
<form>
<input name="foo2" value="<?php echo htmlspecialchars($_POST['foo']); ?>"/>
</form>

So, here's what FINALLY worked. I just rebuilt the second form...:
<form name="testyboy" method="post" action="insert.php">
<input type="hidden" value="1" name="start">
<input type="hidden" value="<?php print $timenotstarted?>" name="testyboy">
<input type="submit" value="Start Timer">
</form>
It's identical. I have no idea what made it work....
Anyways thanks for everyone's efforts! Its much appreciated.

Related

can I use $_POST 2 times with different page?

can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>

Passing form inputs from different pages to submit at final page

I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>

get submit form action with array

i have a form based on database mysql ,just like this
<form name="" id="form1" method="GET" action="submit.php"></form>
<?php
include 'connection.php';
$myquery="SELECT * FROM `tbl` " ;
$params=mysql_query($myquery) or die (mysql_error());
while ($param=mysql_fetch_assoc($params))
{?>
<input name="<?php echo $param['id'] ?>" value="<?php echo $param['child_id'] ?>" />
<input type="checkbox" name="checkbox<?php echo $param['id'] ?>" value="Yes" form="form1" />
<input class="form-control text-center" form="form1" name="desc<?php echo $param['id'] ?>" value="<?php echo $param['desc'] ?>"/>
<input class="form-control text-center" form="form1" name="value <?php echo $param['id'] ?>" value="<?php echo $param['value'] ?>"/>
<select class="form-control" form="form1" id="" name="select<?php echo $param['id'] ?>">
<option value=""></option>
<option value="OK">OK</option>
<option value="NOK">NOK</option>
</select>
<?php } ?>
<button type="submit" class="btn btn-success btn-s-md btn-rounded" form="form1"><i class="icon-save"></i>Save</button>
how can i submit (do update ) to mysql database with this form , i am a newbie in php .
thanks
So yes you have to add the balise form at this end. Then I advice you to add a hidden value with your id.
<input type="hidden" name"id" value="<?php echo $param['id'] ; ?>">
After ad in your page submit.php,
<?php
include 'connection.php';
if (isset($_POST["id"])){
$id=$_POST["id"];#so you can recover your id
$myquery="UPDATE ... " ;
mysql_query($myquery) or die (mysql_error());
echo "update success";# you can add whatever you want here
}?>
You have to wrap all your form elements into the <form> tag. So place your closing tag (</form>) at the end of your form.
<form name="" id="form1" method="GET" action="submit.php">
<input name="..." />
<select>
<option value="...">...</option>
...
</select>
<button>...</button>
</form>
By the way: PHP's mysql-functions are deprecated. Use mysqli instead!

access $_GET value after form submission

example.php gets value of x by $_GET['x'] from another page.
I have a form in the same page (example.php). Upon submission of the form, I am loosing the value of x.
My question is: How can I keep and access the value of x after submission of the form.
Code is like this:
<body>
<?php
if(isset($_GET['y'])) {
$y = $_GET['y']; // I have also tried with $_REQUEST
}
if($_POST['submit']) {
$x = $_POST['field1'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="example.php" method="post">
<input type="text" name="field1" />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body
I have also tried the following as per suggestion, but its not working:
<body>
<?php
if(isset($_POST['submit'])) {
$x = $_POST['field1'];
$y = $_POST['y'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="dollar_get_and_form1.php" method="post">
<input type="text" name="field1" />
<input type="hidden" name="y" value="<?php htmlentities($_GET['y']) ?>" />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body>
Issue is solved by the following codes:
<body>
<?php
if(isset($_GET['x'])) {
$x = $_GET['x'];
echo $x;
}
if(isset($_POST['submit'])) {
echo $_POST['xValue'].'<br>';
echo $_POST['y'];
}
?>
<form name="form1" action="example.php" method="post">
<input type="text" name="y" />
<input type="submit" name="submit" value="Echo the name" />
<input type="hidden" id="xValue" name="xValue" value="<?php echo $x; ?>"/>
</form>
</body>
Thanks to all for your suggestions. If there are better ways to do this, please suggest so.
Your form is POST not GET so the variable will be gone. The best way to get the desired result is through a hidden form field:
<input type="hidden" name="y" value="<?=htmlentities($_GET['y'])?>" />
Then it will be available as $_POST['y'] when the user submits the form
your form's method is post so, there is not $_GET, and $_GET don't work here...
where is $_GET['y'] comming from?
As HTTP is stateless protocol, Get/Post value persistence is limited to last page and current page. You have to make you use of one of techniques :
"hidden field","session" ,"cookie" or writing/reading to tmp. file to preserve value/state.
use get instead of post method='get'; and create a field(hidden) y like following
<body>
<?php
if(isset($_GET['y'])) {
$y = $_GET['y']; // I have also tried with $_REQUEST
}
if($_GET['submit']) {
$x = $_GET['field1'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="example.php" method="get">
<input type="text" name="field1" />
<input type="hidden" name="y" value=1 />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body

Problem with displaying correct hidden field

This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>

Categories