I have a form with a submit button and a handler that stores data in the database. Problem is when the form is submitted, all data is cleared from the input fields. Is there a way to still show them after submit? What changes do I need to make to my form_submit function?
function mymodule_form_submit($form, &$form_state) {
//how to retain the input in the form
}
I'm looking for the most "drupalish" way to get this done?
As indicated by this previous StackOverflow question you can accomplish this with $form_state['storage'] and $form_state['rebuild'].
You can access the data using $_REQUEST['form_variable_name'] where form_variable_name is the name of the html input tag.
You then need to render the page back putting this value into the input tags value field.
<form method="POST" action="/account/contactdetails/">
<div>
<label>First name:</label>
<input type="text" name="firstname" value="<?php echo $_REQUEST['firstname']; ?>" />
</div>
<div>
<label>Last name:</label>
<input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
</div>
<input type="submit" value="Save" />
</form>
Related
How to keep user's input after they submit a form?
For example:
<form action="a.php" method="POST">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
echo $firstname.$lastname;
?>
if user key in fname and lname, yes, it will echo out.. but i wan to keep their first name and last name at the input type, so that user dont need to refill again if the form is long.
You want to make your form sticky so that the user does not need to fill the entire form again which is a good practice.
You can add value to your form inputs for your form example like shown below:
<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])) echo $_POST['firstname'];?>" />
<input type="text" name="lastname" value="<?php if(isset($_POST['lastname'])) echo $_POST['lastname'];?>" />
In the similar way, you can make your entire form sticky (different for textarea and others but the similar idea).
I am using dynamic form where user add more input text boxes for a certain field he want and the name of each box change with an increment like:
<form method="post" action="somescript.php">
<input type="text" name="textbox" />
<input type="text" name="textbox1" />
<input type="text" name="textbox2" />
<input type="text" name="textbox3" />
.... and so on
</form>
I want to echo these data following a loop:
<?PHP
$k=$_POST['counter']; //counter value coming as post variable
for($i=1$i<=$k;$k++){
echo $_POST['textbox'.$i]; //something like this......?
}
?>
Please reply.
Use array notation instead.
<form method="post" action="somescript.php">
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox][" />
.... and so on
</form>
When the form is submitted, $_POST['textbox'] will then be an array, and you can loop over it:
foreach ($_POST['textbox'] as $textbox) {
echo $textbox;
}
I just came across this issue because I had blocks of data that needed to be created dynamically and
echo $_POST["textbox$i"];
worked without the concatenation in it. Let me know if this is bad practice, it works in my situation though. The array way didn't work for me. Sorry for posting this on a 3 year old question. I'm not sure if that's bad practice. Thanks.
I am new to coding and have learned a whole lot in the last 12 months using S/O so i would like to say thanks for helping me out and any input is more than appreciated.
I have created a 4 page Registration form that consists of the following process:
1. index.php (Main page / Landing page with form input values)
2. page2.php (Select date, time and confirm appointment)
3. page3.php (Booking confirmed please Select nearest location, Survey and service)
4. SSL Payments page with iframe insert from 3rd party
on page3.php i have inserted an internal iframe that is hosted on the same server, using separate css files, js, ect.. So due to my lack of coding skills i found it easier to insert the iframe rather than merging the page as 1 complete page.
Now on all pages i have managed to get the $_POST Variables passing from page 1 --> 2 --> 3
But on page 3 i fail to get all the variables displayed in the iframe hidden inputs. I can not understand why only some are being displayed and others are not.. When the form gets submitted the only ones that come through are the ones you can see hidden in source code.
i have also tried to do the following:
insert <?php session_start();?> at the top of the page
and at the bottom of page i have:
<?php
$_SESSION['Name']=$_POST['Name'];
$_SESSION['Postcode']=$_POST['Postcode'];
$_SESSION['Building']=$_POST['Building'];
$_SESSION['EmailAddress']=$_POST['EmailAddress'];
$_SESSION['Telephone']=$_POST['Telephone'];
$_SESSION['datepicker']=$_POST['datepicker'];
$_SESSION['timeofsurvey']=$_POST['timeofsurver'];
$_SESSION['PropertyOwner']=$_POST['PropertyOwner'];
?>
I have these on each page to make sure they get passed across each page, All the input ['names'] are correct and on the same page where the iframe is they are displayed back to the user in a text box to the right side of the iframe basically to show the user that their information is correct while booking their appointment.
Here is my iframe form source code:
<div id="app" my-app my-controller="Controller">
<form action="Processing.php" id="regForm" name="regForm" method="post">
<input type="hidden" value="<?php echo $_SESSION['Name'];?>" name="Full Name">
<input type="hidden" value="<?php echo $_SESSION['Telephone'];?>" name="Telephone">
<input type="hidden" value="<?php echo $_SESSION['EmailAddress'];?>" name="Email">
<input type="hidden" value="<?php echo $_SESSION['Building'];?> " name="Building Number or Name">
<input type="hidden" value="<?php echo $_SESSION['Postcode'];?>" name="Postcode">
<input type="hidden" value="<?php echo $_SESSION['PropertyOwner'];?>" name="PropertyOwner">
<input type="hidden" value="<?php echo $_POST['datepicker'];?>" name="Survey Date">
<input type="hidden" value="<?php echo $_POST['timeofsurvey'];?>" name="Survey Time">
The only hidden variables failing to display are the following:
['Name']
['EmailAddress']
['datepicker']
['timeofsurvey']
All other variables are being displayed fine, As i said even the page that has the iframe inserted is also displaying all variables back to the user correctly. If you click to view page source code you see them fine... Then as soon as you view the inner iframe source code you see the variables above are missing.
This is the outcome when viewing the iframe source code:
<div id="app" my-app my-controller="Controller">
<form action="Processing.php" id="regForm" name="regForm" method="post">
<input type="hidden" value="" name="Full Name"> **<----- This input is missing**
<input type="hidden" value="0123456789" name="Telephone">
<input type="hidden" value="" name="Email"> **<------- This input is missing**
<input type="hidden" value=" " name="Building Number or Name">
<input type="hidden" value="NW1 1AA" name="Postcode">
<input type="hidden" value="Yes" name="PropertyOwner">
<input type="hidden" value="" name="Survey Date"> **<--- This input is missing**
<input type="hidden" value="" name="Survey Time"> **<--- This input is missing**
<input type="hidden" name="field_passthrough1" value="Consumer" />
<input type="hidden" name="field_passthrough2" value="en-UK" />
I hope i have not gone the longest way round here trying to explain, as i understand a lot of you guys are really to busy to be reading long posts like mine when it will be something so simple.. I have spent 2 days trying all sorts to get this working and i can not seem to get a way round this.
I would really appreciate any help guys, Thank You.
############# UPDATE TO LAMONDE ################
This is how i now have the iframe and the only fields that are now missing is:
['Name']
['EmailAddress']
['Building']
---> ['datepicker'] and ['timeofsurvey'] are now working...
<input type="hidden" value="<?php echo $_SESSION['Name'];?>" name="Name">
<input type="hidden" value="<?php echo $_SESSION['Telephone'];?>" name="Telephone">
<input type="hidden" value="<?php echo $_SESSION['EmailAddress'];?>" name="EmailAddress">
<input type="hidden" value="<?php echo $_SESSION['Building'];?> " name="Building">
<input type="hidden" value="<?php echo $_SESSION['Postcode'];?>" name="Postcode">
<input type="hidden" value="<?php echo $_SESSION['PropertyOwner'];?>" name="PropertyOwner">
<input type="hidden" value="<?php echo $_SESSION['datepicker'];?>" name="datepicker">
<input type="hidden" value="<?php echo $_SESSION['timeofsurvey'];?>" name="timeofsurvey">
Here is source view on page3.php iframe:
<input type="hidden" value="" name="Name">
<input type="hidden" value="07541258585" name="Telephone">
<input type="hidden" value="" name="EmailAddress">
<input type="hidden" value=" " name="Building">
<input type="hidden" value="saas" name="Postcode">
<input type="hidden" value="No" name="PropertyOwner">
<input type="hidden" value="21/01/2015" name="datepicker">
<input type="hidden" value="4:30 pm" name="timeofsurvey">
Your $_POST will have the same name has your input.
So: $_POST['Name'] != $_POST['Full Name'];
It's pretty much the same for every input that are missing.
Also you really shouldn't use spaces in input names
I have a form that when they submit it, it stores the info in the database. I need to be able to get the form data to come up on redirect page.
It does not need to fetch the database as I would love to do this PHP style. So lets say they enter there name and city. When they click submit it redirects them to a thank-you page with the results from the form on that page.
In a form, you have each element have a name, lets say name="username", in the php, yould get the value of this as either a get, or a post response, depending on the method of the form.
HTML Form
<form action="process.php" method="get">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
or
<form action="process.php" method="post">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
process.php
$someusername = $_GET['username'];
$someusername = $_POST['username'];
Form page:
<form action="thanks.php" method="post"><input type="text" name="myname" placeholder="Name" /><input type="text" name="mycity" placeholder="City" /><input type="submit" value="submit"></form>
PHP Page
print "Thanks, ".$_POST['myname']." who lives in ".$_POST['mycity']."!";
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.