Multiple forms and one PHP receiver - php

I have MySQL generating forms on one page with the same action and submit button. The number of forms vary. They all call the same PHP file when submitted. Also, I have one PHP file which collects the data upon submission. See the example below.
The problem is when one of the forms is submitted, values get confused with different fields from different forms. Example: When form1 is submitted, PHP receives the form6's values.
How can I make sure each form submits its own values?
HTML Code Example:
<form method="POST" action="index.php?action=newhistory" name="history_1">
<input type="hidden" name="id" value="1">
<input type="text" name="history">
<input type="submit" name="add_history" value="Submit">
</form>
<form method="POST" action="index.php?action=newhistory" name="history_6">
<input type="hidden" name="id" value="6">
<input type="text" name="history">
<input type="submit" name="add_history" value="Submit">
</form>
PHP Code Example:
case 'newhistory':
$id = $_POST['id'];
$history = $_POST['history'];
$sql = mysql_query("INSERT INTO history (id, history) VALUES('".$id."', '".$history."')", $link);
break;
Any solutions?

It will submit the content encapsulated by the <form></form> tags. Having several form's action attribute point to the same page should not create the problem you describe.
The code you wrote here looks fine. Check your HTML code, and ensure you have the corrent <form></form> tags surrounding the elements of each form.

Please use different names for the submit button, then check.
if(isset($_POST('add_history'))
{
}
if(isset($_POST('add_history1'))
{
}

If you hit submit in one form, the browser is supposed to send only the values from this form. When form1 is submitted, PHP will NOT receive the form6's values. Maybe you didn't close the form tag properly or have any JavaScript going wild.

Related

HTML update input textarea everytime "Submit" it clicked

<html>
<script>
function changeText()
{
document.getElementById("input1").value = <?php echo '"'.$_POST['input'].'"'; ?>;
return true;
}
</script>
<form name="mainform" action="" method="post">
<input type="text" name="input" id="input1" />
<input type="submit" onclick = "changeText()" name="Submit" value="Submit!" />
</form>
<html>
i have this code here. can you make it work as intended ?
everytime i click Submit! i want to change the value of the textarea to the last input the user inserted.
PHP code is parsed by a PHP interpreter before any HTML output is sent to the browser.
If your form action is the same page and the same form will be shown before and after submission, then you can let PHP print the value of the input field directly into it.
<input type="text" name="input" id="input1" value="<?php echo htmlspecialchars($_POST['input']);" />
If you're trying to revert the value of this input field whenever a user clicks the submit button, then your code (even if it's prone to code injection) should work but this is useless since the page will be requested again when submit is clicked.
I assume you need to fill in
action=""
By the name of your file, like
action="myFile.php"
Few tips :
NEVER trust the user. The user can manually change the value of the input and send some dangerous values in your $_POST variable. You need to check it using filter_input() by example.
Like #Charles said this is pretty simple problem, use google next time.Here for example

How to get a form name in php script?

For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.

Retrieve Values with Post method from Separate Form Fields in PHP

This might be a basic question of HTML but I'd like to know if it is possible in PHP. Usually when building a form submission page, the form tag is set per one form group. So the submit button is assigned per one form tag. The elements of form fields have to be close as enclosed in the form tag. If I want more complicated layouts, then is it possible to separate form tags to divide form fields?
For example,
<?php
print_r($_POST);
?>
<form name="test" action="" method="post">
First name: <input type="text" name="firstname" value="" /><br />
<input type="submit" value="submit" />
</form>
<p>some other contents</p>
<form name="test" action="" method="post">
Last name: <input type="text" name="lastname" value="" />
</form>
In this case, even if the second field is filled, the value won't be sent. If possible, I'd like to have just one submit button and control multiple forms.
Wrap your entire page into a single form and use some kind of deliminator for your elements (for instance "form2_field1"). I have never found having multiple forms useful (even if there are hide/unhide values) and my guess is that the submit button will probably only submit the form it is wrapped in. As one of the comments has mentioned, use JQuery if you want more complex forms. However, my recommendation is just to send over the entire form, whether it is complex or not and process it according to what you want.

How to call PHP function?

Suppose I have two form in individual pages. One is Called ParytForm.html and second one is clientForm.html.
And I have a single PHP file which contains two php functions. One is PartyForm() and second one is clientForm().
Now is it possible, when user fill the form form PartyForm.html then partyForm() is called. and when user fill the form from clientForm.html then
clientForm() is called.
Thanks
In one form put
<input type="hidden" name="from" value="ParytForm.html" />
and in the other:
<input type="hidden" name="from" value="clientForm.html" />
then in your PHP code distinguish your input:
if ('clientForm.html' == $_POST['from'])
{
// do stuff
PartyForm();
}
else if ('ParytForm.html' == $_POST['from'])
{
// do other stuff
clientForm();
}
I adapted ParytForm.html for fun ;-)
You can send a variable by GET method ("do=partyform"), check it in php and call right function.
You can do this multiple ways, one way is to include a hidden field in each form, and then use a conditional after you submit to run the right function.
Your html code below
<form action="action.php" method="post">
...
<input type="hidden" name="formName" value="PartyForm">
<input type="submit" value="Submit">
</form>
<form action="action.php" method="post">
...
<input type="hidden" name="formName" value="ClientForm">
<input type="submit" value="Submit">
</form>
and then in your action.php or whatever you call the page you submit to
if($_POST['formName'] == "PartyForm"){
partyForm();
}else if$_POST['formName'] == "ClientForm"){
clientForm();
}
To do this, you will need some way of the PHP program to distinguish between the two requests. This can be done by adding a parameter to the URL the form is submitting to, or changing the name of the submit button. I tend to prefer the URL method though, because it is cleaner.
for example
Form 1 could be
<form method="phpfilename.php?dofunction=1">
Form 2 could be
<form method="phpfilename.php?dofunction=2">
Then, on your PHP file, you could check which form is sent using
$form = $_GET['dofunction'];
if ($form == '1') doFunction1();
else if ($form == '2') doFunction2();
The best way to do this (I think) is to use the PHP include() function. Use your forms inside a .php file (which can include HTML only), and include the functions file in both form files.
Then, just use action="clientForm()" to call the function when the form is submitted.

How to check if a form is submitted via javascript?

I have this conventional submit button which submit a form like this:
<form method="post" id="form_submit">
...
<input class="button" type="submit" name="Submit" value="Submit">
</form>
And I check if the submit button is clicked using this:
if(isset($_POST['Submit'])){
//update DB
}
Now I have a submit link using jquery:
Submit
JS code:
$("#form_submit").submit();
What is the alternative way here to be used here for if(isset($_POST['Submit'])) since I'm submitting the form using javascript?
If I understand you correctly, try this:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// your code.........
}
You should add a hidden input <input type="hidden" name="formsubmit" value="yes" /> to the form which will always get submitted, and check for that instead of the button (which only gets submitted if it is clicked on ..)
If I understood your problem correctly that you can simply change input type to hidden.
<form method="post" id="form_submit">
...
<input type="hidden" name="Submit">
</form>
$_POST['Submit'] variable will be defined.
The best solution is "Don't do that". If you want to submit a form then use a submit button (don't do it as a side effect of clicking on a hyperlink to the top of the page). Any JavaScript you want to run can then be handled in the form's submit event.
If you really want to do it as a side effect, then check for the existence of any other field that you know will be set. You could add a hidden field to ensure there will be one of a given name/value combination if you like.

Categories