Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 08-27-2010, 05:49 AM
LeonBelmontX LeonBelmontX is offline
Junior Member
 
Join Date: 08-27-10
Posts: 3
iTrader: 0 / 0%
Question Stuck trying to set cookies from a form

I've recently been using PHP for the first time, and I've managed to carry values from one page to another by appending it to the end of the URL and then placing it in the next page via PHP. However, I'm trying to do something more complicated now, and I'm fairly sure cookies are my only option. I have little to no knowledge of JavaScript, am new to PHP, and I've never worked with cookies before. I've had a fair amount of experience with HTML coding though - I'm just branching out into new things.

Basically, the concept is this:
The site is a fast food retailer with multiple outlets, and the idea is that the user enters their postcode, and a list of stores that are in range of their location is generated in a drop down list.

From this list, the user chooses the store they wish to shop from, then clicks to continue to the main online shop to order.

The value chosen from the list needs to be stored, so that later on it can be inserted into code elsewhere in order for an email detailing the order to be generated and sent to a certain email address depending on which store was chosen.

In terms of coding, what I'm trying to do is this:

Firstly, the user enters their post code into a form and presses submit. This value is appended to the end of the URL when they are moved to the next page.

On the next page, PHP takes the stored post code value, then checks it against several arrays (lists of post codes available for each store). For each store that matches the postcode, the store name is printed on screen, generating a list of stores in range of that post code.

Now, I intend to alter the code so that the PHP generates HTML coding so that the matching stores create a drop-down list as part of a form. The problem is, once I have my drop-down list, I can't figure out how I can then store the user's selected outlet for use in the code later on.

I assume that I should be able to set a cookie containing the value of the selection of the drop down once the submit button is pressed, and then retrieve this cookie when needed.

However, I can't seem to find any help on how to set a cookie that will take the form value and create a cookie with that value - I've either found that methods don't seem to work, don't match what I want to do, or I simply don't understand the code.

Any tips? I essentially just want to know how to create a cookie containing a single form input value once the submit button is pressed.
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 08-27-2010, 06:23 AM
LeonBelmontX LeonBelmontX is offline
Junior Member
 
Join Date: 08-27-10
Posts: 3
iTrader: 0 / 0%
Quote:
<?php
$postcode=$_POST['postcode1'];

$crawley = array("CM1", "CM2", "CM3", "CM4", "cm1", "cm2", "cm3", "cm4");
$braintree = array("CM4", "CM5", "CM6", "CM7", "cm4", "cm5", "cm6", "cm7");
$allcodes = array("CM1", "CM2", "CM3", "CM4", "CM5", "CM6", "CM7", "cm1", "cm2", "cm3", "cm4", "cm5", "cm6", "cm7");

if (in_array("$postcode", $allcodes)) {
echo "Please select the nearest store to your location<form><select>";
}

if (in_array("$postcode", $crawley)) {
echo "<option>CRAWLEY</option>";
}

if (in_array("$postcode", $braintree)) {
echo "<br /><option>BRAINTREE</option>";
}

if (in_array("$postcode", $allcodes)) {
echo "<input type='submit' value='Submit' />
</select></form>";
}

else {
echo "Sorry, there are no stores local to your area";
}
?>

</select>
</form>
By the way, here's my code. The PHP generates a form correctly, but the Submit button needs to be changed to incorporate whatever function or code I need to run in order to take the value chosen and create a cookie.
 
Reply With Quote
  #3  
Old 08-27-2010, 02:36 PM
Luxmo's Avatar
Luxmo Luxmo is offline
Member
 
Join Date: 07-08-10
Location: City of Industry, California
Posts: 36
iTrader: 0 / 0%
Save cookie with postcode:
PHP Code:
$postcode=htmlentities($_POST['postcode1']);
$timetodie 12800// will expire in 12,800 seconds
setcookie('your_cookiename'$postcode, (time()+$timetodie), '/'''0); 
retrieve the cookie:
PHP Code:
if(isset($_COOKIE['your_cookiename'])){
   
$thepostcode htmlentities($_COOKIE['your_cookiename']);

Technically you don't need to append anything to the end of url's (using _GET method) to do this, you could simply use post method (better for SEO anyways).

Further Info:
PHP.Net Cookie Features
PHP Cookie Tutorial @ W3Schools
 
Reply With Quote
  #4  
Old 08-31-2010, 01:27 AM
LeonBelmontX LeonBelmontX is offline
Junior Member
 
Join Date: 08-27-10
Posts: 3
iTrader: 0 / 0%
Thanks for the response - but I'm still a little confused. How exactly do I link the save cookie PHP to the submit button? The code you sent makes sense to me (sort of), but I don't really understand where it goes in the overall coding of my page.

I assume the retrieve function simply works between a couple of PHP tags to insert the value, but I can't work out how I link the submit action with the first bit of code.

Thanks

Edit: Also, I may be wrong but it looks as though the PHP saves the postcode1 value from the start of the document, before the user has selected an option from the form. Since one postcode can relate to more than one of the options, I really need to save the value that is chosen in the form (currently CRAWLEY or BRAINTREE) so that the site can pick up this value later on.

Just to clarify, I am passing values from one page to another twice - this page recieves a value from a previous page (where it retrieves the 'postcode1' value), and I need it to then save a different value from the form selection as a cookie for use elsewhere.

Hope I don't sound too demanding/rude - I've just been stuck on this for quite a while and feel that I'm so close to grasping it!

Last edited by LeonBelmontX; 08-31-2010 at 01:37 AM.
 
Reply With Quote
  #5  
Old 08-31-2010, 08:44 AM
Luxmo's Avatar
Luxmo Luxmo is offline
Member
 
Join Date: 07-08-10
Location: City of Industry, California
Posts: 36
iTrader: 0 / 0%
The form:
Code:
<form name="frmMyForm" method="post" action="mypage.php">Please select the nearest store to your location:<br /><select name="postcode1"><option value="CRAWLEY">CRAWLEY</option><option value="BRAINTREE">BRAINTREE</option></select><br /><input type="submit" name="subMySubmit" value="Submit" /></form>
Code to save the data to a cookie:
PHP Code:
if(isset($_POST['subMySubmit'])){
   
$postcode=htmlentities($_POST['postcode1']);
   
$timetodie 12800// will expire in 12,800 seconds
   
setcookie('your_cookiename'$postcode, (time()+$timetodie), '/'''0);  
   echo 
'Cookie saved.';

Retrieve that cookie:
PHP Code:
if(isset($_COOKIE['your_cookiename'])){
   
$thepostcode htmlentities($_COOKIE['your_cookiename']);
   echo 
'You selected: '.$thepostcode;

It seems you don't understand how to process forms using PHP. Here's a good tutorial exactly for that: http://www.w3schools.com/php/php_forms.asp
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookies for Comments AmySueN Blogging Forum 7 03-24-2010 05:08 AM
Cookies!!! m42 Forum Lobby 46 07-16-2008 02:00 PM
Stuck on a singular form keyword in the rankings rthgreen SEO Forum 2 04-14-2008 12:17 PM
Help me! something about cookies chatoman Coding Forum 0 08-08-2005 07:23 AM
Reseting cookies with PHP bytech Coding Forum 1 07-22-2004 09:21 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 09:26 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC