manipulate form input variable before post - php

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!

Related

Pass Value between pages in 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.

PLS HELP PHP syntax

Here's my form
<form id="place-bid" action="placebid.php?placeBidProductId='.$row['product_id'].'" method="post" >
Your Bid:<br>
<input type="text" placeholder="$$$" name="money">
<input type="Submit" class="button_bid" name="submit" value="Place Bid">
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
</form>
and here is my placebid.php
<?php
// $product_id=$_GET['placeBidProductId'];
$product_id=$_POST['row_id'];
echo " Id Produs : ".$product_id;
?>
My problem: I can't get the value from $row['product_id'], it will only echo the string "$row['product_id'] .$product_id;"
I'm guessing that before you are outputting that form you are doing some sort of query of a database. If that is the case then you likely want to change this line
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
to
<input type=hidden id='rowid' value="<?php echo row['product_id'];>" name='row_id'>
Use print_r($_POST) for all form post value on form action page i.e. placebid.php. it will show you all value in form. Then make you business logic accordingly.

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

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

Form submit on same page with parameters - GET Method

Here is my code:
<form action=\"index.php?m=".$mm."&y=".$y."\">
<input type=\"submit\" value=\"<\">
</form>
The variables $mm and $y are initialized and have a value.
But I am only getting redirected to:
index.php?
What did I do wrong?
Submitting a form with method="get" (the default) will replace any query string in the action with one generated by the form data.
Store your m and y values in <input type="hidden" …> elements instead.
That is because you need to echo the values of $mm and $y
<form action="index.php?m=<?php echo $mm;?>&y=<?php echo $y;?>">
<input type="submit" value="" />
</form>

Categories