Echoed form data and manupulation with GET and POST in PHP - 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;
?>

Related

How do I change a string line submitted in the form with another form input

In my first page I have this code:
$number="1234567891";
$str="456";
echo "<form action='edit.php' method='POST'><input type='hidden' name='msg' value='$message' />
<input type='hidden' name='text' value='$number' />
<input type='hidden' name='edit' value='$str' />
<input type='submit' name='chedit' value='Go' style='position:relative; top:25px; left: 50%;'>
</form>";
In my edit.php I have this code:
<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="$mumu"/>
<input type="submit" name="pch" value="Change"/>
</form>
<?php
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
$obinna = str_replace("$change","$mumu","$text");
echo $obinna;
}
}
?>
My problem is that whenever I put a new text in new form and click submit to edit a character in the old string submitted line the page refreshes and no result is output. Please can anybody sort this out?
// try this ..
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
//456 , //555(post value) , //(your text)12345678910
// $obinna = str_replace("Set old value you change in text","set new value you want to set in text ","your orignal text ");
$obinna = str_replace("$mumu","$change","$text");
echo $obinna;
}
}
echo '<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="" placeholder="Change"/>
<input type="text" name="edit" value="" placeholder="Edit"/>
<input type="submit" name="pch" value="Submit"/>
</form>'
Check Demo Url :- https://eval.in/931366

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>";
}

Get variable from another conditional block

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

Categories