in PHP CodeIgniter application there are several pages with input text boxes and text areas.
How to implement the session save in php i.e. whnever the user closes the tab,
the entered data should remain saved until the user logs out of the application.
or is there any plugin available to achieve the same ?
my code for the same :-
<form accept-charset="UTF-8" method="post" id="myform" action="/saveform">
Name: <input name="name" type="text" value="" /><br />
Address: <textarea name="address"></textarea><br />
Gender: <select name="gender">
<option value="M">Male</option>
<option value="F">Female</option>
</select><br />
Interests:<br />
1: <input name="interests[]" type="text" value="" /><br />
2: <input name="interests[]" type="text" value="" /><br />
3: <input name="interests[]" type="text" value="" /><br />
4: <input name="interests[]" type="text" value="" /><br />
5: <input name="interests[]" type="text" value="" /><br />
Sign up for our newsletter: <input name="signup" type="checkbox" value="1" /><br />
<input name="submit" type="submit" value="Submit" />
</form>
You can save the form data whenever the tab or page is closed, using AJAX you can save all data in the session and then in form you can add modify like this:
<form accept-charset="UTF-8" method="post" id="myform" action="/saveform">
Name: <input name="name" type="text" value="<?php echo isset($_SESSION['user']['name']) ? $_SESSION['user']['name'] :"";?>" /><br />
Address: <textarea name="address"><?php echo isset($_SESSION['user']['address']) ? $_SESSION['user']['address'] :"";?></textarea><br />
Gender: <select name="gender">
<option value="M" <?php echo $_SESSION['user']['gender']=='M' ? "selected" :"";?>>Male</option>
<option value="F" <?php echo $_SESSION['user']['gender']=='F' ? "selected" :"";?>>Female</option>
</select><br />
Interests:<br />
1: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_0']) ? $_SESSION['user']['interests_0'] :"";?>" /><br />
2: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_1']) ? $_SESSION['user']['interests_1'] :"";?>" /><br />
3: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_2']) ? $_SESSION['user']['interests_2'] :"";?>" /><br />
4: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_3']) ? $_SESSION['user']['interests_3'] :"";?>" /><br />
5: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_4']) ? $_SESSION['user']['interests_4'] :"";?>" /><br />
Sign up for our newsletter: <input name="signup" type="checkbox" value="1" <?php echo $_SESSION['user']['signup']=='1' ? "checked" :"";?> /><br />
<input name="submit" type="submit" value="Submit" />
</form>
Hope this helps !!
Related
I'm totally new to php and I am trying to dynamically change the form displayed when the user makes a choice on a drop down list. I can get the drop down list to show but it does not display anything when I submit.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Forms</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
If (!isset($_GET['userChoice']))
{ //open the if block to write the html to the screen
?>
<h1>Show your html option form</h1>
<form method="GET" action="thirdtry.php" name="userChoice">
<select size="1" id="choice" name="choice">
<option selected="selected">Choose which form you would like</option>
<option value="feedback">Feedback Form </option>
<option value="help">Help Request Form</option>
</select>
<input type="submit">
</form>
<?php
}//close the if block
else{ //show feedback
if($_GET['userChoice']=="feedback")
{
?>
<h1>Show the feedback form</h1>
<form id="fb" name="fb" method="post" >
<fieldset>
<label>First Name:
<br />
<input type="text" name="fName" id="fName" />
</label>
<br />
<label>Last Name:
<br />
<input type="text" name="lName" id="lName" />
</label>
<br />
<label>Street Address:
<br />
<input type="text" name="address" id="address" />
</label>
<br />
<label>City
<br />
<input type="text" name="city" id="city" />
</label>
<br />
<label>State
<br />
<input type="text" name="state" id="state" />
</label>
<br />
<label>Postcode
<br />
<input type="text" name="postcode" id="postcode" />
</label>
<br />
<label>Country
<br />
<input type="text" name="country" id="country" />
</label>
<br />
<label>Email Address:
<br />
<input type="text" name="FromAddress" id="FromAddress" />
</label>
<br />
<label>Your message:
<br />
<textarea name="message" rows="7" cols="30"></textarea> <br />
</label>
<div>How many stars would you give this site? <br />
(We love 5 stars!)<br />
<input type="radio" name="rating" value="1" />1<br />
<input type="radio" name="rating" value="2" />2<br />
<input type="radio" name="rating" value="3" />3<br />
<input type="radio" name="rating" value="4" />4<br />
<input type="radio" name="rating" value="5" />5<br />
</div>
<br />
<input type="submit" />
<input type="reset" />
</fieldset>
</form>
<?php
}
else
{//must be help form
?>
<h1>Show the help form</h1>
<form id="help" method="post" >
<fieldset>
<label>First Name:
<br />
<input type="text" name="fNameHelp" id="fNameHelp" />
</label>
<br />
<label>Last Name:
<br />
<input type="text" name="lNameHelp" id="lNameHelp" />
</label>
<br />
<label>Street Address:
<br />
<input type="text" name="addressHelp" id="addressHelp" />
</label>
<br />
<label>City
<br />
<input type="text" name="cityHelp" id="cityHelp" />
</label>
<br />
<label>State
<br />
<input type="text" name="stateHelp" id="stateHelp" />
</label>
<br />
<label>Postcode
<br />
<input type="text" name="postcodeHelp" id="postcodeHelp" />
</label>
<br />
<label>Country
<br />
<input type="text" name="countryHelp" id="countryHelp" />
</label>
<br />
<label>Email Address:
<br />
<input type="text" name="FromAddress" />
</label>
<br />
<label>Your message:
<br />
<textarea name="messageHelp" rows="7" cols="30"> </textarea> <br />
</label>
<div>How Would you like to be contacted?<br />
(Choose all that apply) <br />
<input type="checkbox" name="ckemail" id="ckemail" value="email" onclick="return validateHelpForm()"/>email<br />
<input type="checkbox" name="cktelephone" id="cktelephone" value="telephone" onclick="return validateHelpForm()"/>telephone<br />
</div>
<br />
<input type="submit" />
<input type="reset" />
</fieldset>
</form>
<?php
}//close the if block
}
?>
</body>
</html>
You're checking for the wrong variable:
If (!isset($_GET['userChoice']))
should be
If (!isset($_GET['choice']))
Also, why are you capitalizing If? (That won't cause an issue - it's just weird ;)
Well first off, do you want the page to refresh, or would you like it to change dynamically without a refresh, using Javascript. From your brief description, I am thinking you want the change to happen without a refresh.
And being a "novice", I would advise you use Jquery as your Framework as opposed to full blown javascript. Here is what I think you're trying to accomplish.
Since PHP is a server-side programming language, you can't dynamically change a webpage without a refresh. Javascript and Jquery are client-side, so many things can happen in your browser without the need or a refresh.
LINK: Here is the example I just created.
////////jquery/javascript///////
$('#changeme').change(function(){
var value = $('#changeme').val();
if(value == 1){
$('#dis1').show();
$('#dis2').hide();
}
else if(value == 2){
$('#dis1').hide();
$('#dis2').show();
}
else{
$('#dis1').hide();
$('#dis2').hide();
}
});
////////HTML///////
<select id="changeme">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<div style="display:none;" id="dis1">Displaying 1</div>
<div style="display:none;" id="dis2">Displaying 2</div>
I have this wizard that needs to display the previous posted value
index.html
<form method="post" action="posted.php">
<input type="text" name="surname" value="" placeholder="Surname" />
<input type="text" name="firstname" value="" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
in the posted.php i have a similar form only this time i know the value from $_POST
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
I am having a hard time trying to come up with a solution that shows what checkbox was checked.I have seen several solutions like https://stackoverflow.com/a/11424091/1411148 but i wondering if there more solution probably in html5 or jquery.
How can i show what checkbox was checked?.The probem i am having is that <input type="checkbox" name="jquery" checked /> checked checks the checkbox and no post data can be added to show what the user checked.
This would be a way to go:
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" <?php if (isset($_POST['php'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="jquery" <?php if (isset($_POST['jquery'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="python" <?php if (isset($_POST['python'])) echo 'checked="checked"'; ?> />
<input type="submit" value="Submit" />
</form>
Ok so Im trying to use radio buttons in my PHP code. I wrote a Madlib in PHP and I want to write in a radio button that when a user selects it, it will change the story etc. Happy ending or sad ending here the code I have so far. Any help would be appreciated.
<html>
<head>
<title>James Nygaard's Mablib</title>
</head>
<body bgcolor="<?php echo $bg; ?>" text="<?php echo $fg; ?>">
<h1 style="font-family:cursive;">Create your Madlib below:</h1>
<form action="" method="post">
Enter the name of a boy:
<input name="noun1" type="text" value="<?= $_POST['noun1'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj1" type="text" value="<?= $_POST['adj1'] ?>" /><br />
Enter the name of a man:
<input name="noun2" type="text" value="<?= $_POST['noun2'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj2" type="text" value="<?= $_POST['adj2'] ?>" /><br />
Enter the name of a woman:
<input name="noun3" type="text" value="<?= $_POST['noun3'] ?>" /><br />
Enter your favorite animal:
<input name="noun4" type="text" value="<?= $_POST['noun4'] ?>" /><br />
Enter a name:
<input name="noun5" type="text" value="<?= $_POST['noun5'] ?>" /><br />
Enter the name of your favorite city:
<input name="noun6" type="text" value="<?= $_POST['noun6'] ?>" /><br />
Enter a feeling that ends in "ness":
<input name="adj3" type="text" value="<?= $_POST['adj3'] ?>" /><br />
Enter a Verb that ends in "ing":
<input name="verb2" type="text" value="<?= $_POST['verb2'] ?>" /><br />
Enter the name of a boy:
<input name="noun7" type="text" value="<?= $_POST['noun7'] ?>" /><br />
Enter the name of a girl:
<input name="noun8" type="text" value="<?= $_POST['noun8'] ?>" /><br />
<input type="submit" value="Click here or press enter to see your MadLib" /><br />
</form>
<div style="color: #2F4F4F; font-family: cursive;">
<?php
$bg = "8FBC8F";
$fg = "2F4F4F";
if(isset($_POST['noun1'])) {
echo "<h1> The adventures of {$_POST['noun1']}. </h1>";
echo "This is where i will put the story. I already have the story but the code is really long so I left it out of this question.";
echo "<br> The end.";
}
?>
</div>
</body>
</html>
It's not especially clear in this small space, but I tried to follow your code style.
Select an option:
<label>
<input name="some_option" type="radio" value="option_1" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_1') echo 'checked="checked" '; ?>/> Option 1
</label>
<label>
<input name="some_option" type="radio" value="option_2" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_2') echo 'checked="checked" '; ?>/> Option 2
</label>
If you run that code you should see what's going on.
I have a form:
<form action="<?php echo the_permalink(); ?>" method="POST">
<input type="text" name="name" value="" placeholder="Your First and Last Name *" />
<?php echo $firstnameError; ?>
<input type="text" name="email" value="" placeholder="Yoyr Email" />
<?php echo $emailError; ?>
<br>
<input type="text" name="company" value="" placeholder="Your Company Name" />
<input type="text" name="phone" value="" placeholder="Your Phone number" />
<textarea name="project" rows="4" cols="50" placeholder="Describe the scope of work and the most important features of the project *'"></textarea>
<?php echo $addressError; ?>
<br>
<input type="radio" name="budget" value="1500" />
<input type="radio" name="budget" value="2500" />
<input type="radio" name="budget" value="5000" />
<input type="radio" name="budget" value="10000" />
<input type="radio" name="budget" value="100001" />
<input type="radio" name="budget" value="not sure" />
<input type="hidden" name="submit" value="1" />
<input type="submit" value="SUbmit" />
</form>
It actions to the same page, but when I do print_r($_POST); it does not print anything, i.e. no value in $_POST.
What could be the reason(s) for this? I studied a few questions on SO on this but none gave me the answer I was looking for.
If your passing the name as a Post value, wordpress DOSNT like this!
change this
<input type="text" name="name" value="" placeholder="Your First and Last Name *" />
to
<input type="text" name="thename" value="" placeholder="Your First and Last Name *" />
changing the name to thename, Will work guaranteed! ;)
<form action="<?php the_permalink(); ?>" method="POST">
You don't need to echo the_permalink().
This works for me:
<?php print_r($_POST);?>
<form action="" method="POST">
<input type="text" name="name" value="" placeholder="Your First and Last Name *" /><?php echo $firstnameError; ?><input type="text" name="email" value="" placeholder="Yoyr Email"/><?php echo $emailError; ?><br>
<input type="text" name="company" value="" placeholder="Your Company Name"/><input type="text" name="phone" value="" placeholder="Your Phone number"/>
<textarea name="project" rows="4" cols="50"placeholder="Describe the scope of work and the most important features of the project *'"></textarea><?php echo $addressError; ?><br>
<input type="radio" name="budget" value="1500" /><input type="radio" name="budget" value="2500" /><input type="radio" name="budget" value="5000" /><input type="radio" name="budget" value="10000" /><input type="radio" name="budget" value="100001" /><input type="radio" name="budget" value="not sure" />
<input type="hidden" name="submit"value="1"/>
<input type="submit" value="SUbmit" />
</form>
change this
acton="<?php echo the_permalink(); ?>"
to
action="<?php echo the_permalink(); ?>"
I found same problem in my project and solution is in $_POST, maybe you use lower case in your code, change it to upper case.
change $_post to $_POST !
SOLUTION:
due to request problems, in wordpress sites instead of
<form action="http://example.com/">...
You may need to point the .php file.. example:
<form action="http://example.com/index.php">...
//////p.s. echo is automatically done with the_permalink() [same is: echo get_permalink()
First of all thanks in advance, this has been very frustrating and I'm hoping someone can see something I'm not, I am definitely no php expert. Well here' what is going on.
I have a form where I have a checkbox for people to opt in to our newletter. The form element looks like this:
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
That is then submitted to a php file with this code:
if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes'){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
$newsletter is then inserted into a database field.
The issue is that whether the box is checked or not it is being sent to php as true, so every entry is receiving the newsletter.
Any help would be greatly appreciated! Thanks!
Here's the full form minus the option list for the sake of brevity
<form method="post" action="contact.php" name="contactform" id="contactform">
<fieldset>
<legend>Please fill in the following form all fields are required, thanks!</legend>
<label for=firstName accesskey=F><span class="required">*</span>First Name</label>
<input name="firstName" type="text" id="firstName" size="30" value="" />
<br />
<label for=lastName accesskey=L><span class="required">*</span>Last Name</label>
<input name="lastName" type="text" id="lastName" size="30" value="" />
<br />
<label for=email accesskey=E><span class="required">*</span>Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<br />
<label for=city accesskey=C><span class="required">*</span>City</label>
<input name="city" type="text" id="city" size="30" value="" />
<br />
<label for=state accesskey=S><span class="required">*</span>State</label>
<select name="state" type="text" id="state">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
<br />
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
<br />
<p><span class="required">*</span> Are you human?</p>
<label for=verify accesskey=V> 3 + 1 =</label>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
Your code is correct. You most likely have a problem with your database insert/update logic. Why do you assume it is the PHP form handling?
This has just dawned on me.
If a checkbox is unchecked it isn't set in the $_POST superglobal. So if !isset($_POST['newsletter']) then it wasn't checked - if isset($_POST['newsletter']) it was checked.
Edit: Remove the 'yes' part - the value will never be yes, just true or 'on'.
Edit 2:
I've tested this to death. Change your code to:
if (isset($_POST['newsletter'])){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
Remove the value="Yes" attribute from your checkbox input also. If you want it checking by default use checked="checked".