Hold variables in php for submit - php

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.

Related

How can I update the right data by using id?

I can display first name, last name and the instrument they borrowed. There is a drop down if they want to change the instrument they borrowed. Why is my code not changing the instrument that the person is changing? What could be wrong in this code? Please help. I know my code is not agreeable to sql securement, this is just an activity. database
<html>
<head>
</head>
<body>
<form method="post">
<?php
$con=mysqli_connect("localhost","root","","borrow");
$q="SELECT students.studentid,students.studfname,students.studlname,instruments.instrumentname, instruments.instrumentid from students INNER JOIN student_instrument ON students.studentid=student_instrument.id INNER JOIN instruments ON student_instrument.checkoutdate=instruments.dateacquired";
//SELECT students.studentid,instruments.instrumentid,students.studfname,students.studlname,instruments.instrumentname from
//students INNER JOIN instruments ON students.studentid=instruments.instrumentid
$t="SELECT instrumentname from instruments where dateacquired='avail'";
$r=mysqli_query($con,$q);
$e=mysqli_query($con,$t);
while($result=mysqli_fetch_array($r)){
$id=$result['instrumentid'];
$studfname=$result['studfname'];
$studlname=$result['studlname'];
$instrumentname=$result['instrumentname'];
echo $studfname." ".$studlname." ".$instrumentname." ";
echo '<input type="hidden" name="id" value='.$id.'>';
echo '<select name="inst">';
while($f=mysqli_fetch_array($e)){
$avail=$f['instrumentname'];
echo '<option>'.$avail.'</option>';
}
echo '</select>';
echo '<input type="submit" value="submit" name="submit"><br>';
}
?>
</form>
<?php
if(isset($_POST['submit'])){
$id=$_POST['id'];
$inst=$_POST['inst'];
$p="UPDATE instruments SET instrumentname='$inst' WHERE id='$id'";
$q=mysqli_query($con,$p);
if($q){
header('location:student.php');
}
}
?>
</body>
</html>
Check the generated HTML code and look for the <form> and </form> tag. Currently you have a form similar like this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
<input type="hidden" name="id" value="8" />
<input type="submit" />
<input type="hidden" name="id" value="9" />
<input type="submit" />
<input type="hidden" name="id" value="11" />
<input type="submit" />
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
You generate only one form. When you submit that form you will send all the ids on the same field name "id". This means only the last id will be stored in $_POST['id']. This explains the "the last row is updated, not the first one".
To fix your problem you have to create individual forms so they will look similar to this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="8" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="9" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="11" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
So opening and closing the form must be happening inside the while loop.

How to get customized attributes value of a html radio button in php

<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>

Double Get Send Data

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>

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']:''); ?>" />

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..

Categories