Send a form using jQuery and check if it was submitted - php

I have 2 forms on my page and 1 of them has 2 different ways to be submited, a submit button and a jQuery on click event, it looks something like this:
<script>
$(document).ready(function() {
$('#img_send_form').click(function() {
$('#form2').submit();
});
});
</script>
<form name="form1" action="#" method="post">
<input type="text" name="field1"/>
<input type="submit" name="send1"/>
</form>
<form name="form2" action="#" method="post">
<input type="text" name="field1"/>
<input type="text" name="field2"/>
<input type="text" name="field3"/>
<input type="text" name="field4"/>
<input type="text" name="field5"/>
<input type="text" name="field6"/>
<input type="text" name="field7"/>
<input type="submit" name="send2"/>
</form>
<img src="xxx" id="img_send_form"/>
What is the best way to check if form2 was submmitted on php? Do I need to use isset for every form field ?
if (isset($_POST['field1'])||isset($_POST['field2'])||isset($_POST['field3'])||isset($_POST['field4'])||isset($_POST['field5'])||isset($_POST['field6'])||isset($_POST['field7']))
or is there another "better" way to do it?

Take Hidden Field with Same Name in Both Forms (but differ Ids if you need)
Then you will only need to check the that hidden field

just add a hidden field to the second form, and in PHP check if it's set, in this case was used the second form

Not necessary to take hidden fields,
PHP :
if(isset['send2'])) { echo "Form2 submitted !" ;?> }

<script>
$(document).ready(function() {
$('#img_send_form').click(function() {
$('#form2').submit();
});
});
</script>
<form name="form1" action="#" method="post">
<input type="text" name="field1"/>
<input type="submit" name="send1"/>
</form>
<form name="form2" action="#" method="post">
<input type="hidden" name="form2_send"/>
<input type="text" name="field1"/>
<input type="text" name="field2"/>
<input type="text" name="field3"/>
<input type="text" name="field4"/>
<input type="text" name="field5"/>
<input type="text" name="field6"/>
<input type="text" name="field7"/>
<input type="submit" name="send2"/>
</form>
<img src="xxx" id="img_send_form"/>
And php :
if(isset['form2_send'])) { echo "Form2 submitted !" ;?> }

Related

How to Append/Insert new hidden input into form PHP

I have a form request in different page of my script. Example like this one
<form action="/action_page.php" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">
<input type="submit" value="Submit">
</form>
I want to add a hidden input in my form so it will be like this
<form action="/action_page.php" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="hidden" id="id" name="id" value="randomnumber">
<input type="submit" value="Submit">
</form>
How to do this in PHP after page successfully access, and not inside each html, so I can confirm that each form of my page has input hidden id
<input type="hidden" id="id" name="id" value="randomnumber">
Found a way,
In my last code before html show, I have to add this
echo '<script>var form_id = document.createElement("input");
form_id.setAttribute("type", "hidden");
form_id.setAttribute("name", "form_id");
form_id.setAttribute("value", "form_id");
document.querySelector("form").appendChild(form_id);</script>';
Hope this help other, better than just make sense

why my forms are not working in bootstrap website?

I tried all but no form is working with method or simple action like http://google.com `
<form action="http://google.com" method="post">
<input type="text" />
<input type="submit" />
</form>
even this is not working
use this
<form role="form" action="http://google.com" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" />
</form>
role attribute for FORM
name attribute for input
In PHP, $_POST always accept name attribute from form elements:-
You need to write
<input type="text" name='name_of_your_field' />
instead of
<input type="text" />
You need to refer this Link.

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

Why is this form making the fields and variables part of the process form?

I am doing this:
<form action="processform.php" type="post">
<input type="text" name="sample">
<input type="text" name="how">
<input type="checkbox" name="fields" value="fields">Something</input>
</form>
When the submit button is hit, this is what shows in the address bar of the process page:
https://www.domain.com/processform.php?sample=&how=&fields=
Change to <form method="post" />
change type="post" to method="post" for starters
The method attribute specifies the submission type (e.g. "get" or "post"), but you've used the type attribute...and this is incorrect. You need something like:
<form action="processform.php" method="post">
<input type="text" name="sample">
<input type="text" name="how">
<input type="checkbox" name="fields" value="fields">Something</input>
</form>
Your markup is wrong
Try
<form action="processform.php" method="post">
<input type="text" name="sample" value='VALUE' />
<input type="text" name="how" value='VALUE' />
<label for='fields'>Something</label>
<input id='fields' type="checkbox" name="fields" value="Something" />
</form>
<form action="processform.php" method="post">
type needs to be method

PHP/HTML form submission

I have the HTML and PHP code
<form name="addaserver" method="post" action="addaserver.php">
<p>Server Name<form method="post">
<input name="servername" type="text" /></form>
<p>Description<form method="post">
<input name="description" type="text" /></form>
<p>Server IP<form method="post">
<input name="ip" type="text" /></form>
<p>Tags (ex: "pvp, economy, fun")<form method="post">
<input name="tags" type="text" /></form>
<form method="post">
<input name="submitserver" type="submit" value="submit" /></form>
</p>
and
(addaserver.php)
$servername=$_POST['servername'];
$desc=$_POST['description'];
$ip=$_POST['ip'];
$tags=$_POST['tags'];
Obviously I'm trying to get the data from the forms...however when I click "submit" it just reloads the page the forms are on. It's probably just some simple error, but I can't figure out what's wrong D:
You're supposed to define only one form, not one for each input:
<form name="addaserver" method="post" action="addaserver.php">
inputs, inputs, inputs, submit
</form>
First thing I see wrong is that you have two separate form tags in the same HTML.
The second one is pretty much useless as it provides no data to any target or action. I would restructure your HTML to be more like this and try it;
<form name="addaserver" method="post" action="addaserver.php">
<p>Server Name<form method="post">
<input name="servername" type="text" /></p>
<p>Description<form method="post">
<input name="description" type="text" /></p>
<p>Server IP<form method="post">
<input name="ip" type="text" /></p>
<p>Tags (ex: "pvp, economy, fun")
<input name="tags" type="text" /></p>
<p><input name="submitserver" type="submit" value="submit" /></p>
</form>
Also take note of the fact that I got rid of all your closing form tags as they would have caused issues too. You only need one closing tag at the very outside most segment of your form's body as shown in the code sample too.
You have way too many <form method="post"> tags in your code.
Your code should start with <form method="post"> and end with </form>, but in between there should only be input fields.
You define action to 'addaserver.php' in the first <form> tag, but the submission button is after a different <form> tag so it doesn't respect that initial target you are setting.
You seem to be enclosing all your input element in different tags. a Form tag is a collection of Form elements that will have their values submitted when the form is submitted. And if you do not specify the action attribute on a form it will (as you say) reload the page. So in the above example if you remove all the tags surrounding the input tags and put them all under the same tag you should get your information posted
Look at http://www.w3schools.com/html/html_forms.asp and http://www.tizag.com/phpT/examples/formex.php for examples on how to do this.
Hope that makes sense.
You only need one form tag for the whole form to submit
<form name="addaserver" method="post" action="addaserver.php">
<p>Server Name
<input name="servername" type="text" /></p>
<p>Description
<input name="description" type="text" /></p>
<p>Server IP<form method="post">
<input name="ip" type="text" /></p>
<p>Tags (ex: "pvp, economy, fun")
<input name="tags" type="text" />
<input name="submitserver" type="submit" value="submit" /></form>
</p>
You're nesting extra form tags throughout your form. You only need one form tag. All of the inputs go inside it.
<form name="addaserver" method="post" action="addaserver.php">
<p>Server Name</p>
<input name="servername" type="text" />
<p>Description<</p>
<input name="description" type="text" />
<p>Server IP</p>
<input name="ip" type="text" />
<p>Tags (ex: "pvp, economy, fun")</p>
<input name="tags" type="text" />
<input name="submitserver" type="submit" value="submit" />
</form>
Try this instead:
<form name="addaserver" method="post" action="addaserver.php">
<p>Server Name: <input name="servername" type="text" /></p>
<p>Description: <input name="description" type="text" /></p>
<p>Server IP: <input name="ip" type="text" /></p>
<p>Tags (ex: "pvp, economy, fun")<input name="tags" type="text" /></p>
<p><input name="submitserver" type="submit" value="submit" /></p>
</form>

Categories