Values sent to PHP via POST are all empty strings - php

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" />

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

Form action html to php

i have
<form action = "https://creator.zoho.com/api/xml/fileupload/scope=creatorapi&authtoken=**************" method="post" enctype="multipart/form-data">
<input type="text" name="applinkname" value="sample">
<input type="text" name="formname" value="Employee">
<input type="text" name="fieldname" value="File_upload">
<input type="text" name="recordId" value="1235667754455">
<input type="text" name="filename" value="profile.jpg" >
<input type="file" name="file">
<input type="submit" value="Update">
</form>
But i must send 2500 files, how can i transform this html to php, my problem is <input type="file" name="file">, how convert it to give a value in php ?
thanks you
You can use $_REQUEST['FILE']=$SOME VARIABLE;
And name is same what you use in your database
then apply enctype="multipart/form-data".

mysql insert from generated names

I have a form:
<form>
<input type="text" name="aaa" id="aaa">
<input type="submit">
</form>
I have also jQuery script and can append this input.
Example:
Generating name.
<form>
<input type="text" name="aaa" >
<input type="text" name="aaa2" >
<input type="text" name="aaa3" >
<input type="submit">
</form>
Problems is MySQL insert:
How to insert from generated names?
I know $_POST['aaa'], but I don't know from generated names.
If you're generating dynamically text boxes from jQuery and you want to push them to mySQL, instead of creating them with names like 'aaa', 'aaa2' or 'aaa3' you can use square brackets like this:
<form method="POST">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<button type="submit">Send</button>
</form>
That will allow you to use "field_name" as an array of strings.
<?php
$field_name = $_REQUEST['field_name'];
foreach($field_name as $value) {
$query = sprintf("INSERT INTO table SET field_name = '%s'", $value);
/* Execute Insert Query */
}
OK. if i have form like this:
<form method="POST">
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
and i apend 3 input's
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
<button type="submit">Send</button>
</form>
in mysql i have table:
id
aaa
bbb
ccc
this is one insert

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

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