简体   繁体   中英

PHP: Form not echoing back cookie value for field

I have a field on a form that uses a jQuery plugin to create an autocomplete drop-down list. I call it via the following AJAX code:

$(function() {
   $("#fieldName").autocomplete({
   source: "projects.php",
   minLength: 3,
   select: function(event, ui) {
   $('#fieldName').val(ui.item.value);
} 
});

When the user submits the form, the action (update.php) takes him to a review page. If he needs to make a change, he just clicks the BACK button on his browser to go back to the form. The problem is that the "fieldName" field is blank when the user clicks back, as if the function is re-initializaing the field. I've tried both setting session variables and cookies in update.php to capture the user-supplied field value, and I can confirm that they're being set by echoing them on the review page, but when I try to echo the values on the form, nothing happens. Is there a way to maintain the state of "fieldName" when the user clicks BACK?

I've tried the following so far:

setcookie("cookieName", $_POST['fieldName']); //Set in update.php
<input type="text" name="fieldName" id="fieldName" value="<?php echo $_COOKIE['cookieName']; ?>" /> 
<!--Set on form -->

And the session variable attempt:

$_SESSION['fieldValue'] = $_POST['fieldName']; //Set in update.php
<input type="text" name="fieldName" id="fieldName" value="<?php echo $_SESSION['fieldValue']; ?>" /> 
<!--Set on form -->

Try axing the value property of your text input. There is no reason to explicitly set it to do what you're trying to do. Browsers should keep the values entered into text fields unless they are changed by input or the DOM. Since no event or method would clear the field, it should retain its value even between page transitions, unless some event or method on the source page actually is setting a default value for it.

Also, if you're using an old version of JQuery, you might take a look at the article below. There was an issue some years ago where under certain conditions autocomplete-enabled fields would NOT retain their values as expected when revisted via the browser's back button. See:

http://bugs.jqueryui.com/ticket/7790

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM