Double Get Send Data - php

In this page : page.php?id=value
I've this html's code:
<form action="" method="get">
<input type="text" name="key" />
<input type="submit" name="send />
</form>
It's redirect me to: page.php?key=value , i want to redirect to: page.php?id=value&key=value , how i can do it? I must redirect it to this page with PHP ?

simply,
<form action="page.php" method="get">
<input type="hidden" name="id" value="<?php echo $value ?>">
<input type="text" name="key" />
<input type="submit" name="send" />
</form>

You can have the id as a hidden input in your form
<form action="" method="get">
<input type="hidden" value="<?php echo $my_id; /*Suppose this variable contain your value */ ?>" name="id" />
<input type="text" name="key" />
<input type="submit" name="send" />
</form>

put everything you want on the next page in your form:
<form action="page.php" method="get">
<input type="text" name="id" value="<?php echo $_REQUEST['id'];?>" />
<input type="text" name="key" />
<input type="submit" name="send />
</form>
Really though, you should be using POST to send data, and the above code is NOT secure (allows anything in a url to easily end up in a form that could create some sql injection or XSS type issues.

You need to have the form's action set to the page you want it to submit to. If it is blank it will just submit to the same page.
<form action="page.php?id=value&key=value" method="get">
<input type="text" name="key" />
<input type="submit" name="send />
</form>

Related

How to keep form data even after submiting not to dispear

In this
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
After I submit the form, the page refreshes and the data inside the input texts gets blank
Is it possible to keep the data even after submit?
Regards.
You can simply use ajax for submitting the form.
Or use following
<form method="POST" action=""><input type="text" class="field small-field" name="tex1" value="<?php (isset($_POST['text1]))? echo $_POST['text1] : '';" /><input type="submit" value="search" name="search"/><input type="submit" value="print" name="print"/></form>
try to echo, what ever is the variable named for your input.
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1'];?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
With php for example:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1']; ?>"/>
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
If you are handling the post on the same page you could just do like this on the fields where you want the posted value to be shown:
<input type="submit" value="search" name="search" <?php if( isset( $_POST['search'] ) ){ echo "value=\"". $_POST['search'] ."\"; } ?>/>
Use this:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php if(isset($_POST['tex1'])) echo $_POST['tex1'] ?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
Bascially http is statelessprotocol , hence you need to save the data some where
The simplest way in this case would be to use a conditional operator
<input type="text" class="field small-field" name="tex1" value="<?php echo (isset($_POST['search'] || $_POST['search'] )?$_POST['tex1']:''); ?>" />

PHP Form URL vars

I was wondering if it would be possible to have a variable in the URL created when submitting a form?
form:
<form class="register_form" action="action.php" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
I'd like the link to end up as: http://.../action.php?do=register
My reasoning for this is so that I can use action.php for more than one thing using if statements. Thanks ^^
Just append the variable you want to the action link.
<form class="register_form" action="action.php?do=register" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
Or you can add a hidden field to your form:
<input type="hidden" name="do" value="register" />
Sure, the form action URL can have a query string:
<form class="register_form" action="action.php?do=register" method="POST">
The form data will be sent via POST but do will still be available via GET.
You need to add this to the form
<input type="hidden" name="do" value="register">
Yes, it is possible. You can use any one of following methods
1) You can set the name of your submit button "do"; As the value of your submit button is "Register"
<input type="submit" name="do" value="Register" />
OR
2) You can add a hidden field to your form
<input type="hidden" name="do" value="register" />

Reading a form in wordpress with the plugin Exec-PHP

We are using the Exec-PHP to use PHP in our wordpress page. But we got a problem, we cant read from a textfield form. Is this a Exec-PHP limmitation (If so could anyone recommend a plugin who does allow this) Or is something wrong in our code?
This is our code:
<form action="" method="post">
<input type="text" value="voornaam" />
<input type="submit" value="Verzend" />
<input type="hidden" name="button_pressed" value="1" />
<?php
if(isset($_POST['button_pressed']))
{
echo $_POST["voornaam"];
echo 'Done';
}
?>
You should add:
<input type="text" value="voornaam" />
Then, in your PHP, you can use:
echo $_POST["voornaam"];
Your form:
<form action="" method="post">
<input type="text" value="voornaam" />
<input type="submit" value="Verzend" />
<input type="hidden" name="button_pressed" value="1" />
</form>

How to process multiple forms using one php script

I have multiple forms and I have one php script that I want to use to process these forms but when I click on submit for any of the forms...the script is processed by the number of forms with the submit button named 'submitForm' in this case, the alert will show 3 times instead of once! What am I not doing right?
NB. I hope this makes much sense?
html code
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
php script
<?php
if (isset($_POST['submitForm'])) {
echo('<script>alert("Form Submitted")</script>');
}
?>
when I click on submit for any particular form, it submits all the forms.
this is not true.
Once your forms have proper formatting, your browser will submit only current one.
(and PHP has nothing to do here)
however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.
Also note that none of your text fields being sent to the server as they have no names.
I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!
well, it seems you want to ask how to distinguish these forms.
add a hidden field into each
<input type="hidden" name="step" value="1" />
and then in PHP
if ($_POST['step'] == 1) {
//first form
}
if ($_POST['step'] == 2) {
//second
}
This submits one form of many to php. Copy, paste, test, and study.
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:
Array
(
[A] => e
[B] => f
[C] => g
[D] => h
[submitForm] => Submit Form
)
#Jay
Actually its not hard.
Once you supply form names, your work is done. the DOM does the rest.
write one php block to do your functions (create/update/retrieve/delete)
Whichever button is clicked, by default it submits only the elements enclosed together with it.
if(!empty($_POST)){
if(isset($_POST['submit'])){
print "<pre>";
var_dump($_POST); // write your code here as you would
print "<pre>";
}
}
try this with your form above.
I know this is an old post but here's how I solve this very problem.
All you need to do is make sure the submit buttons in each form have different names. Eg:
<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>
Then, you simply check which form's submit button was pressed.
<?php
if (isset($_POST['submitForm1'])) {
echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) {
echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) {
echo('<script>alert("Form 3 Submitted")</script>');
}
?>
If you need dynamic forms, you may try below code. While statement can be changed to fetch data from DB and use foreach instead. Hope you know this.
Here, I used while($n<10) for 10 dynamic forms.
You can also use tag as below if you need separate form names.
<form action="" name="form<?=$n?>" method="post">
This will create separate form names such as form1, form2, etc but not necessary here.
<?php
if (isset($_POST['submitForm'])) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
$n=0;
while($n<10) {
$n++;
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<?php
}
?>
Sample page with output when I click row 5..

Hold variables in php for submit

I have several html textareas on my site. Each has a submit button. When a user types in one of the textareas i need to know which textarea this is. These textareas are each assigned a number taken from a mysql database. I can get the numbers out of the database, but how can I make it so that when a user types in a textarea and clicks submit the submit form knows which textarea this is. Please ask to clarify if needed. I tried my best to explain the problem. thanks.
p.s. the submit button just performs a mysql set values query. I'm using php on my site.
for example: a textarea is assigned '3.' When i submit this form i need 3 to be sent into my mysql set values query.
Use a hidden input to store a reference for each form
<input type="hidden" name="database_reference" value="<?php echo $dbId; ?>" />
Then when you submit the form $_POST['database_reference'] gives you the database id.
<input type="hidden" value="5" name="which_one" />
so for example
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="1" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="2" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="3" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="4" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="5" name="which_one" />
<input type="button" />
</form>
UPDATE:
<?php
if ($_POST){
include("db_connection.php");
mysql_query("UPDATE table SET column = '".mysql_real_escape_string($_POST['text'])."' WHERE value = ".intval($_POST['value']));
echo "done";
}
?>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="1" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="2" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="3" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="4" name="which_one" />
<input type="button" />
</form>
<form method="post" action="">
<textearea name="text"></textarea>
<input type="hidden" value="5" name="which_one" />
<input type="button" />
</form>
Assuming you have multiple <form></form> tags set up, one for each <textarea>, I would just add a hidden input field in each form. For example:
<form>
<textarea />
<input type="hidden" value="1" />
<input type="submit" />
</form>
<form>
<textarea />
<input type="hidden" value="2" />
<input type="submit" />
</form>
You can flesh it out from there, but you get the idea.

Categories