How to Append/Insert new hidden input into form PHP - 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

Related

append php form URL

I currently built a simple form to GET request someones zipcode.
<form action="http://example.com" method="get" target="_blank">
<p>ZIPCODE</p>
<input type="text" name="zip">
<input type="submit" value="Submit">
</form>
When submitted it will be http://example.com/?zip=ZIPCODE
What I am looking to do it add an additional piece so it will be http://example.com/?zip=234&country=usa
I tried adding a hidden field <input type="hidden" name="country=usa"></input> but this replaces = with %3D and adds = after it like so: http://example.com/?zip=ZIPCODE&country%3Dusa=
How can I easily append the URL with country=usa?
Try:
<form action="http://example.com" method="get" target="_blank">
<p>ZIPCODE</p>
<input type="text" name="zip">
<input type="hidden" name="country" value="usa">
<input type="submit" value="Submit">
</form>

Send a form using jQuery and check if it was submitted

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 !" ;?> }

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

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