PHP/HTML form submission - php

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>

Related

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 :)

Values sent to PHP via POST are all empty strings

I have the following form:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" id="name">
<INPUT type="text" id="id">
<INPUT type="submit" value="Send">
</P>
</FORM>
Unfortunately, whatever is typed into the text fields, the PHP script receives only empty strings in every $_POST variable. All of the variables are set, just empty. What can be the cause of that?
You are missing the name properties in your html:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" name="name" id="name">
<INPUT type="text" name="id" id="id">
<INPUT type="submit" value="Send">
</FORM>
Only form elements with a name attribute will be sent to the PHP script (same with POST)
Try this:
<form action="http://url.to.mysite.com/doc.php" method="post">
<input type="text" id="name" name="name">
<input type="text" id="id" name="id">
<input type="submit" value="Send">
</form>
You need to add the name attribute to your input tags. Example: <input type="text" name="name" id="name" />

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

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