Get variable from another conditional block - php

I want to get a variable from a conditional if of form assigned to take the value of a textbox:
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name; //i got "Notice: Undefined variable: name" here
}
?>
</form>
I want show value of $name after input:name pressed.

This should solve the issue
$name = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name;
}
The problem in your code is that the scope of $name is limited to the first if

Hello and welcome to stackoverflow,
If you want to make your form in 2 steps, you need to store the "name" value in the intermediate form.
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit']))
{
$name = htmlentities($_POST['name']);
echo "<input type=\"hidden\" value=\"{$name}\" name=\"name\">";
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show']))
{
$name = htmlentities($_POST['name']);
echo $name;
}
?>
</form>
Several things to point out :
in a field of type "hidden" you store your $name
in such a way you can recover it in the second step
you should also have a look to the htmlentities() function
Hope this helps!
Try it here

Related

Echoed form data and manupulation with GET and POST in PHP

Actually I need to echoed the form and get the form data in the same php page or send to another php page,A pseudo code is prepared as i cannot post my original code here .
<?php echo "<html><body>";
if(isset($_POST['submit']))
{
$name = $_POST['firstname'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
echo"<form action=$_SERVER['PHP_SELF']>
First name:<br>
<input type='text' name='firstname' value='John'><br>
Last name:<br>
<input type='text' name='lastname' value='Rambo'><br><br>
<input type='submit' value='Submit'>
</form></body></html>";
?>
The php page is call/loaded successfully loaded .The form is also working.But
form action=$_SERVER['PHP_SELF'] is not working as well as the code below is also not working.
if(isset($_POST['submit']))
{
$name = $_POST['firstname'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
This is tested code. Similar to what your want.
Change $_SERVER['PHP_SELF'] to #
<?php
error_reporting(E_ERROR );
$first = '';
$last = '';
if (intval($_POST['sub'])){
$first = $_POST['firstname'];
$last = $_POST['lastname'];
}
echo <<<EOT
<html><head></head><body>
<form action="#" method="post">
First name:<br>
<input type='text' name='firstname' value="$first"><br>
Last name:<br>
<input type='text' name='lastname' value="$last"><br><br>
<input type='submit' value='Submit'/>
<input type="hidden" name="sub" value=1/>
</form></body></html>
EOT;
?>

add values with one another with input field submit in php calculator

I want a calculator with my PHP code that adds values with one after one with submit button.like when I input a number then submit it and show it on the page then and input other, it should add previous number.and then enter another then submit.like these, numbers are adding with one another after submitting.
<?php
session_start();
?>
<?php
error_reporting(0);
?>
<html>
<title>adding input single values</title>
<body>
<form method="post">
<input type="text" name='number' method="post"/>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number']))
{
}
else
{
$sum += $_POST['number'];
echo ++$sum;
}
?>
</body>
</html>
here is the output
You could use a hidden field instead of storing in the session.
<input type="text" name='number' method="post"/>
<?php
if(!isset($_POST['number'])) {
echo "<input type='hidden' name='prev_number' value=0 />";
} else {
$sum = $_POST['number'] + $_POST['prev_number'];
echo "<input type='hidden' name='prev_number' value=" . $sum . " />";
echo $sum;
}
?>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number'])) {
// ...
}
else {
$_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : '';
$_SESSION['number'] += $_POST['number'];
echo $_SESSION['number'];
}

how to dynamically add / remove form while page refresh using php

How to dynamically add and remove input fields the form will stay while page refresh using PHP?. could it possible to use session? please any PHP script for this? thanks in advance!!
You could use something like this
<?php
$showEnterOther = "";
$showEnterOtherAsWell = "";
if(isset($_POST["show_form"])) {
$showEnterOther = "<input type='text' name='whatever' />";
$showEnterOtherAsWell = "<input type='text' name='whatever' />";
}
?>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter Username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];} ?>"/>
<?php echo $showEnterOther; ?>
<?php echo $showEnterOtherAsWell; ?>
<input type="submit" name="show_form" value="Continue!" />
</form>

dynamic form submitting

I have in the form like
<form action="sub.php" method="post">
<input type="text" name="username[]"><br>
<input type="text" name="hometown[]"><br>
<input type="text" name="country[]"><br>
<input type="submit" value="submit">
</form>
sub.php
$username = $_POST["username"];
foreach($_POST['username'] AS $ID => $Value){
echo "Checkbox with value ".$sValue." was checked!<br>";
}
I could get only one one input field i.e., username
Can we get all 3inputs to sub.php
If I understand the question
<form action="sub.php" method="post">
<input type="text" name="user[1][name]"><br>
<input type="text" name="user[1][hometown]"><br>
<input type="text" name="user[1][country]"><br>
<input type="text" name="user[2][name]"><br>
<input type="text" name="user[2][hometown]"><br>
<input type="text" name="user[2][country]"><br>
<input type="submit" value="submit">
</form>
PHP
$users = $_POST["user"];
foreach($users AS $ID => $info){
echo "user $ID ({$info['name']}) lives in {$info['hometown']}<br>"; // dollar symbol added
}
echo "all usernames: ";
$all_ids = array_keys($users);
foreach($all_ids as $current_id) {
echo $users[$current_id]['name']." ";
}
I'm not sure what your question is but there are a few issues with your html. It should be the following:
<form action="sub.php" method="post">
<input type="text" name="username"><br>
<input type="text" name="hometown"><br>
<input type="text" name="country"><br>
<input type="submit" value="submit>
</form>
I removed the brackets from the fields because brackets normally imply that you want your php code to see it as an array of values but you have single text fields.
If you want to get all of the inputs from the form you should use:
foreach($_POST AS $ID => $Value){
echo "Textbox with value ". $Value ." was used!<br>";
}
I changed it to textbox because your form doesn't have any checkboxes
try this (not elegant but should show you where its going wrong..)
$username = $_POST["username"];
foreach($_POST['username'] AS $ID => $Value){
echo "Checkbox with value ".$Value." was checked!<br>";
}
$hometown = $_POST["hometown"];
foreach($_POST['hometown'] AS $ht_ID => $ht_Value){
echo "Checkbox with value ".$ht_Value." was checked!<br>";
}
$username = $_POST["country"];
foreach($_POST['country'] AS $c_ID => $c_Value){
echo "Checkbox with value ".$c_Value." was checked!<br>";
}
If you have equal # of username,hometown,country and in correct sequence, then you can use following way
foreach($_POST['username'] AS $ID => $Value){
echo "Username ".$Value." was checked!<br>";
echo "Hometown ".$_POST['hometown'][$ID]." was checked!<br>";
echo "Country ".$_POST['country'][$ID]." was checked!<br>";
}

Reply to a message

I have the following code:
<select name="to" class="combo" value='
<?php
if(isset($_POST['reply']))
{
echo "<option value='$reply'>$reply</option>";
}
?>
' />
<?php
$q = $database->selectAllUsersNotMe();
while($row=mysql_fetch_assoc($q))
{
$u=$row['username'];
echo "<option value=\"$u\">$u</option>";
}
?>
</select>
What this does is produce a combo box with a dropdown for all users on my site excluding the user sending the message.
I am trying to add a reply element to the message.
When i click reply, i use the following code:
<? $reply = $_POST['rfrom']; ?>
<form name='reply' method='post' action='/newmessage.php'>
<input type='hidden' name='rfrom' value='<?php echo $pm->messages[0]['from']; ?>' />
<input type='hidden' name='rsubject' value='Re: <?php echo $pm->messages[0]['title']; ?>' />
<input type='hidden' name='rmessage' value='[quote]<?php echo $pm->messages[0]['message']; ?>[/quote]' />
<input type='submit' name='reply' value='Reply' />
</form>
The values are correct and definately pass the information using POST.
On the initial piece of code I provided, how can I alter this so the username that I am replying to is selected when I am replying, if not, the usernames are just listed.
Thanks
$fromname=(isset($_POST['rfrom'])) ? $_POST['rfrom'] : ''; //ought to validate $_POST
while($row=mysql_fetch_assoc($q)) {
$u=$row['username'];
$selected=($u==$fromname) ? 'selected="selected"' : '';
echo "<option value=\"$u\" $selected>$u</option>";
}
$replyUser = $_POST['rfrom'];
while($row = mysql_fetch_object($q))
{
if($row->username == $replyUser)
{
echo('<option value="'.$row->username.'" selected="selected">'.$row->username.'</option>');
}else{
echo('<option value="'.$row->username.'">'.$row->username.'</option>');
}
}

Categories