Hello, I'm tring to input variable into cookie so when I will reload the page the input box will be filled in the variable data. I'm Pretty sure that I do something wrong with the apostrophes: $str = '\''$name = '\''.$name.''; <?PHP if ($_POST) { $name = $_POST['name']; $str = '\''$name = '\''.$name.''; setcookie("test", $str); // echo "str: $str <br />"; } echo $_COOKIE['test']; echo " <form method='post' action='1.php'> <input type='text' name='name' value='$name' /> Name <td><input type='submit' value='Submit' class='submit' /></td> </form> "; ?> PHP: Can you please help me?
what are you trying to do with: $str = '\''$name = '\''.$name.''; ? It should be like this: $str = '$name = '.$name;
I want to put $STR into cookie and when I the page will be loaded the form fields' will be automateclly fiiled from the var in the cookie... Think about form will 10 fields (filter) and I want to save the data for the next time... I want to put all the data in cookie and when the form will be loaded the form fields will be filled
Do you want to print "$name = roice", when you print $STR? if yes then it should be : $str = '$name='.$name;
ah, no - I just want the input box to be auto fiiled (from the cookie): <input type='text' name='name' value='$name' />
I've had to do this with some online games I've made. Checking whether the $_POST global is true is a poor way of determining whether an HTTP request has been sent. Instead, you should have a hidden HTML form element that is called something along the lines of "form sent" and is statically set as "1". Then you should check that this form element has been sent. The reason why this is not working for you is that you create the cookie in the same page instance as you check it. As far as I'm aware you can only access a cookie once it has been set correctly and the session with the web server has changed, or in other words, the page is refreshed. I would create a separate PHP script for creating the cookie then redirect it back, thus avoiding the user refresh requirement. cookie.php <?php # Cookie handling script // First sanitise ALL user input ALWAYS // $formData = CleanFunction($_POST); // Parse post global through user def. func. for cleaning $formData = $_POST; // For simplicity, will use unsanitised data if($formData['fSent'] && !empty($formData['fName'])) { setcookie("cookie", $formData['fName']); // Missing a few parameters here, use more in practice } // Redirect to form script. Note this is HTML. Cannot be done // with PHP header because cookie may have been set print "<head><meta http-equiv='REFRESH' content='0;url=form.php'></head>"; print "Redirecting you. Please click <a href='form.php'>here</a> if you're not redirecting within 5 seconds."; ?> PHP: form.php <?php # Form handling script $cookie = $_COOKIE['cookie']; print "<form method='post' action='cookie.php'> <input type='text' name='fName' value='$cookie'> <input type='submit' value='Go'><input type='hidden' name='fSent' value='1'> </form>"; ?> PHP: If you're going to use more of this type of save-and-display functionality with clients I'd recommend using sessions instead. See here. Hope this helps. I'll be reading through it a few more times and correcting mistakes, bear with me I typed that out off the top of my head. Corrected a few mistakes and tested it on my local machine, works fine.
<? // Getting POST vars and setting the cookie if(isset($_POST[name])) { $str = $_POST['name']; setcookie("test", $str); } // Getting the cookie if(isset($_COOKIE['test'])){ $name = $_COOKIE['test']; } ?> // Auto fill the input box <input type="text" name="name" value="<?=$name?>" /> PHP: This will do...
Webby that solution does not work. You have to refresh the page once again after it has been already submitted to see the change. Read what I wrote about cookie initialisation. A developer should always aim for as much cohesion as possible.
Thanks, But it won't work for me - I'm gonna have 10 fields in my form and I want to put all the variables in 1 cookie and when I'll reload the page I want all the 10 fields to be filled from the variables in the cookie...
simple solution for your problem <? // Getting POST vars and setting the cookie if(!empty($_POST)){ $postdata = serialize($_POST); setcookie("postdata", $postdata); } // Getting the cookie if(isset($_COOKIE['postdata'])){ $post = unserialize($_COOKIE['postdata']); } ?> // Auto fill the input box <input type="text" name="username" value="<?=$post['username']?>" /> <input type="text" name="first_name" value="<?=$post['first_name']?>" /> <input type="text" name="last_name" value="<?=$post['last_name']?>" /> Code (markup):
Use php function explode(); <? // Setting the cookie $str = $_POST['name'].","; $str .= $_POST['anotherVar'].","; $str .= $_POST['anotherVar2'].","; $str .= $_POST['anotherVar3'].","; $str .= $_POST['anotherVar4'].","; $str .= $_POST['LastVar']; setcookie("myCookie", $str); // Getting the cookie if(isset($_COOKIE['myCookie'])){ $str = $_COOKIE['myCookie']; } $var = explode(",", $str); // now var[0] is name, var[1] is anotherVar, var[2] is anotherVar2 and so on ?> // Auto fill the form <input type="text" name="name" value="<?=$var[0]?>" /> <input type="text" name="anotherVar" value="<?=$var[1]?>" /> <input type="text" name="anotherVar2" value="<?=$var[2]?>" /> <input type="text" name="anotherVar3" value="<?=$var[3]?>" /> PHP: You are right about that though. I don't know what his intention to use the cookie is for. If he needs to access cookie on the same page, he can use Javascript's document.cookie to fetch it and populate the form.
Roice, I gave you a solution. Look above. I tested it and it works perfectly and just how you want it. Good point Webby, JS is good to avoid certain PHP flaws.
The only problem with javascript is that it can be altered by end user.. But your solution or the code I gave above, both should work without any need of javascript, I guess.
Hi Webby - Now it's working but - IS it possible to do it without changing the variables name? I mean - instead of using: value="<?=$var[0]?>" I want to keep my previous var: value="<?=$name?>" So my form will looks like this: <input type="text" name="name" value="<?=$name?>" /> <input type="text" name="name" value="<?=$company?>" /> <input type="text" name="name" value="<?=$client?>" /> Is it possible? BRUm - Thank you, I'll try it too...
Just change the variable names. There's no real gain to have them more understandable other than ease of reading. // Declare and define all the temp vars for the array elements $name = $var[0]; $company = $var[1]; $client = $var[2]; // etc... PHP: Then you can apply the names.