Pass Value between pages in Php - php

I want to pass the value from this page to next page pay.php file below is my form (index.php )
<form action=\"/modules/gateways/pay.php\" method=\"post\" id=\"checkout[id]\">
<input type=\"hidden\" name=\"user\" value=\"[userid]\">
<input type=\"hidden\" name=\"decription\" value=\"[itemname]\">
<input type=\"hidden\" name=\"amount\" id=\"amount[id]\" value=\"[price]\">
<input type=\"hidden\" name=\"type\" value=\"deposit\">
</form>
and in pay.php, I am trying to get the value with this code:
$m_amount = number_format($_POST['amount'], 2, ".", "");
$m_desc = strip_tags($_POST['decription']);
I am not getting values in pay.php.

You need to check your HTML.
<form action="/modules/gateways/pay.php" method="post" id="checkout[id]">
<input type="hidden" name="user" value="123">
<input type="hidden" name="decription" value="itemone">
<input type="hidden" name="amount" id="amount_id" value="538">
<input type="hidden" name="type" value="deposit">
</form>
and in pay.php try to receive to simply print POST variable
print_r($_POST);

Include JQuery or a similar JS framework or use native JS to submit this form.
Without a submit-button you have to submit this form by JS or a JS-framework.
In JQuery you have to write something similar to this example:
$('#checkout[FORMID]').submit();
This will submit the form. For sure you have to complete this but basicly this is the missing call.
I saw your form contains hidden inputs. If this form depends on another form/action or else you should either include the hidden form in the depoending form or submit it by itself using JS/JQuery/something similar.

Related

HTML form's "action" doesnt carry over my $_GET data

I have a HTML form that acts as a "confirm deletion?" page and my "Back" button is playing up.
$string = "foo.php?id=" . $_POST['fooid'] . "&id2=" . $_POST['barid'];
<!-- ^^ this ends up being "foo.php?id=1&id2=2" -->
<form action='<?php echo $string; ?>'>
<input type='submit' value='Back'>
</form>
the problem is, when the button is pressed it links to foo.php without any of the $_GET data in my string, even though the string contains "id=1&id2=2"
P.S I changed the code above so people could better understand it, here is the raw code:
<?php
$string = "xrays.php?id=" . $_POST['visitid'] . "&id2=" . $_POST['patientid'];
?>
<form action='delete.php' method='post'>
<input type='hidden' name='xrayid' value='<?php $_POST['xrayid']?>'>
<input type='submit' name='submit' value='Confirm Delete?'>
</form>
<form action='<?php echo $string; ?>'>
<input type='submit' value='Back'>
</form>
Maybe it's not the best answer but I would like do something like that:
$string = "foo.php?id=" . $_POST['fooid'] . "&id2=" . $_POST['barid'];
<!-- ^^ this ends up being "foo.php?id=1&id2=2" -->
<form action='foo.php'>
<input type="hidden" name="fooid" value ="<php echo $_POST['fooid']; ?>" />
<input type="hidden" name="barid" value ="<php echo $_POST['barid']; ?>" />
<input type='submit' value='Back'>
</form>
This should work properly.
EDIT: change $_GET na $_POST
You need to put the get variable in the form hidden inputs, like so:
<form action='foo.php'>
<input type="hidden" name="id" value="1" />
<input type="hidden" name="id2" value="2" />
<input type='submit' value='Back'>
</form>
Or you could use a link:
Back
Let's start with form submission in general. W3C: Form Submission
Next, let's review $_GET and $_POST. PHP Manual: $_GET | PHP Manual: $_POST
In summary, inside of your <form> tag, use either method="get" or method="post". Only one of the superglobal arrays will be populated by successful controls, based upon your method of sending the data. I believe the query string must result from a GET request url (which may be the default), not just a plain string slapped into the action="" attribute. I could be wrong about the query string, but you have another problem. You are using two forms on one page. Presently, I think only one form's controls can be submitted successful at a time.
<form action='delete.php' method='post'> <!-- FORM 1 -->
<input type='hidden' name='xrayid' value='<?php $_POST['xrayid']?>'>
<input type='submit' name='submit' value='Confirm Delete?'>
</form>
<form action='<?php echo $string; ?>'> <!-- FORM 2, add a method="" attribute -->
<input type='submit' value='Back'>
</form>
Upon adding a method="get" to form two, it should become clear that a composite $_POST + $_GET request is not possible in the two form approach, and that you need to start with making a single, monolithic form instead of two modular ones. Using the type="hidden" attribute of an <input /> tag, inside of one form, as in #machineaddict's answer, will help. However, what will really help is if you explicitly use all the correct attributes of each tag so that you can spot errors like this in the future.
In a situation like this, it is helpful to know that the $_SERVER['QUERY_STRING'] element would hold the complete query string if your web server received one.
PHP Manual: $_SERVER

Form submit to another page and then review or write to a file

I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.

manipulate form input variable before post

BEFORE hitting the submit button on a form, I need to send form variables and their values into a php serialized array (name=>value). And then upon post, I need to pass the array to a php function called modify($vararray). For example:
<form action = "someaction.php" method="post"/>
<input type="text" name="var1" id="var1" <br>
<input type="text" name="var2" id="var2" <br>
<?php
(DO SOMETHING HERE TO CREATE A PHP SERIALIZED ARRAY OF NAME=>VALUE INTO $VARARRAY OF ALL PREVIOUS INPUT VARIABLES ABOVE)
echo "input type=\"hidden\" name=\"modvar" id=\"modvar\" value=\"" . modify($vararray) . "\">\n";
?>
<input type="submit" name="submit" value="Submit"/>
</form>
Any suggestions?
You should use JQuery.serializeArray() for this here you can find an example:
URL: .serializeArray()
I hope this helps!

hidden attribute in input tag from html form

I am trying to get the posted information and display the info using the following code:
PHP code:
$self = $_SERVER['PHP_SELF'];
if(isset($_POST['send'])){
$words = htmlspecialchars($_POST['board']);
print "<b>".$words."</b>";
} ​​​​
HTML code:
<form action="<?php $self ?>" method=post> <!--$self is the directory of the page itself-->
<p><i>Comment</i></p>
<textarea name="board" rows="20" cols="10"></textarea>
<input name="send" type="hidden" />
<p><input type='submit' value='send' /></p>
</form>
The code above will work as I intented. However, if I get rid of the input name="send" type="hidden", the user input message will not show up once the send button is clicked. Why would this happen?
You need to add name='send' to your submit button, your PHP code is reading the name of the form elements, and you have not specified one for your submit button.
<form action="<?php $self ?>" method=post> <!--$self is the directory of the page itself-->
<p><i>Comment</i></p>
<textarea name="board" rows="20" cols="10"></textarea>
<p><input type='submit' name='send' value='send' /></p>
</form>
Also, a quick note - you can change your form method to GET instead of POST to easily see what form data you're sending in the URL bar.
This is because you are checking that the POST variable "send" isset. That is what you named your hidden input.
You should add a name to your submit input. Example:
<p><input type='submit' name="submit_button" value='send' /></p>
Now in your php, check for the name of your submit button. I used "submit_button" in this example. Here is modified code example:
$self = $_SERVER['PHP_SELF'];
if(isset($_POST['submit_button'])){
$words = htmlspecialchars($_POST['board']);
print "<b>".$words."</b>";
}
Dont have to bother naming your send button or anything, just remove that hidden line...
and change your php to....
$self = $_SERVER['PHP_SELF'];
if(isset($_POST)){
$words = htmlspecialchars($_POST['board']);
print "<b>".$words."</b>";
}

Query String in form does not work

I have the following form in my form.php file:
<form action="operation.php?part=dictionary&operation=<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>" method="get">
.....
....
</form>
And in my operation.php file:
if($_GET['operation']=='save'){
echo "This is true";
}else{
die(mysql_error());
}
And it shows the message that it does not recognize the operation parameter.
So if anyone have any idea of how to distinguish the operation between save and edit would be really appreciated. Thanks you
You can try using:
<form action="operation.php" method="get">
<input type="hidden" name="part" value="dictionary">
<input type="hidden" name="operation" value="<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>">
</form>
Setting a form's method to "GET" results in ignoring all GET-parameter added to the action of the form. In order to get those parameter to work you will have to add them as hidden input fields otherwise you switch your form's method to "POST". This results in setting POST-parameter according to form fields and setting GET-parameter according to the link additions made at form's action.
You need to use hidden parameters to submit values to your form, like so:
<form action="operation.php" method="GET">
<input type="hidden" name="part" value="dictionary" />
<input type="hidden" name="operation" value="<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>" />
</form>

Categories