如何更新用户插入的值(使用javascript和php)? [关闭]

如何更新用户插入的值(使用javascript和php)?  [关闭]

问题描述:

I have a variable that I'm calling in php and showing in a input field for the user by default, to make some calculations, but the user can change the value. I'm using javascript to get the value, but it doesnt update. Its value always keeps the default value.

我有一个我在php中调用的变量,默认情况下在用户的输入字段中显示 进行一些计算,但用户可以更改值。 我正在使用javascript来获取值,但它不会更新。 它的值始终保持默认值。 p> div>

Um... several things here...

Firstly, we don't have any code. So we have no idea what you're trying to do.

So I'm going to assume you're talking about some sort of global variable (if you just declare $foo = 10 somewhere in your script you won't be able to get it; the script is done parsing by the time the page is outputted).

So let's say you have $_COOKIE['some_value'] as the variable you want to change. The idea is you make an ajax call to some php file that will update it. So you might have

//change_value.php

setcookie($_GET['cookie_name'], $_GET['new_value']);

And you could make an ajax call using a library (jQuery here)

$.ajax({
  url: 'change_value.php',
  data: 'cookie_name=bar&new_value=' + new_value //new value is a variable in javascript,
  success: function(data) {
    alert("Success");
  }
});

You can read more about ajax with jQuery here http://api.jquery.com/jQuery.ajax/

The example above also takes back any return data outputted by change_value.php. So you could update change_value.php to change the value if specified, then output the (new) value regardless (so you could grab it).

Of course, with cookies you could do that directly with javascript, but that's just an example. If you had sessions the same approach could be used.