Let's say I have a form that I send via GET:
<form method="get" action="/search.php?foo=bar&test=1&something=else">
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
And after submitting my form and processing the data (which consists of only saving it to a file), the url is changed to:
search.php?day=test&link=google.com
What should I do so the url becomes:
/search.php?foo=bar&test=1&something=else&day=test&link=google.com
(preserving the old parameters that were included in action attribute.)
The form action will change every time and it's difficult to keep the old GET parameters in the form action.
However, you can go with hidden fields.
Try this:
<form method="get" action="/search.php">
<input type="hidden" name="foo" value="bar"/> <!-- Add this -->
<input type="hidden" name="test" value="1"/> <!-- Add this -->
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
You could try changing the action before submitting, depending how you're going to submit the form. This can be done if you apply IDs to your texts and either a name or an ID to the form.
HTML:
<form id="frm" method="get" action="/search.php?foo=bar&test=1&something=else">
<input type="text" name="day" id="day" placeholder="day"/>
<input type="text" name="link" id="link" placeholder="link"/>
</form>
Then with JavaScript you can run a function and change the form's action:
var _form = document.getElementById('frm');
var day = document.getElementById('day').value;
var link = document.getElementById('link').value;
_form.action += '&day=' + day + '&link=' + link;
_form.submit();
You could try to use a hidden input field.
<input type="hidden" name="name" value="value">
if you are getting your variables first from get and then want to add into 2nd form you could get this and create input fields hidden with these get values
Now when you submit form in get url you will get all you desire data
<?php
if($_GET){
if(isset($_GET['submit1'])){
$foo = $_GET['foo'];
$test = $_GET['test'];
$something = $_GET['something'];
?>
<form method="get" action="/search.php">
<input type="hidden" name="foo" placeholder="foo" value="<?php echo $foo; ?>"/>
<input type="hidden" name="test" placeholder="test" value="<?php echo $test; ?>"/>
<input type="hidden" name="something" placeholder="something" value="<?php echo $something; ?>"/>
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
<?php
}
}
?>
Related
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
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 !" ;?> }
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 :)
I cannot get the $_POST['value'] after form submission.
I have used javascript to assign a value to an input field.
code:
function updateValue(pid, value){
// this gets called from the popup window and updates the field with a new value
document.getElementById(pid).value = value;
}
The above function is called by a popup widow to assign a value to my input field: pid
Here is the form script:
<form action="test.php" method="post">
<input type="text" name="project_name" id="pid" disabled="disabled" />
<input type="submit" id="search" name="search" value="Search" />
</form>
In my test.php, I have done:
include 'functions.php'; //Has the connection script
if (isset($_POST['search'])){
echo $project_id = $_POST['project_name'];
$sql = mysql_query("SELECT * from item
where category like '$project_id %'
");
echo $num = mysql_num_rows($sql);
}
But I am getting the error: Undefined index: project_name
Disabled inputs doesnt post. Use hidden input like this:
<form action="test.php" method="post">
<input type="text" name="project" disabled="disabled" />
<input type="hidden" name="project_name" id="pid" />
<input type="submit" id="search" name="search" value="Search" />
</form>
Try filling the hidden field when you set the value into disabled field
<form action="test.php" method="post">
<input type="text" name="project" id="pid" onchange="document.getElementById("pid-new").value=document.getElementById("pid").value" disabled="disabled" />
<input type="hidden" id="pid-new" name="project_name" />
<input type="submit" id="search" name="search" value="Search" />
</form>
your project_name input field is disabled.
remove this attribute then it will work.
It's because disabled attributes aren't passed along the form requests.
If you're setting the value through javascript, and that part works, replace your text input with this:
<input type="hidden" name="project_name" id="pid" />
this will work
I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.