获取php中的javascript变量[duplicate]

时间:2022-08-27 17:38:09

This question already has an answer here:

这个问题已经有了答案:

Lets say that I have HTML code:

假设我有HTML代码:

<input type="text" id="name" class="form-control" placeholder="Upisi ime">

I get value with php like this: $_POST['name'].

php的值是:$_POST['name']。

But what If I have JavaScript variable:

但如果我有JavaScript变量:

var name = "john";

How can I get this JS variable with PHP to use for adding to database?

如何让这个带有PHP的JS变量用于添加到数据库?

3 个解决方案

#1


0  

You can declare these variables via input box (even hidden input) and using jQuery to grab all of these variables from the input boxes. It'll then seralize them and send them as an object to PHP via $_POST[]; If using this method, jQuery will be already doing the ajax work for you.

您可以通过输入框(甚至是隐藏的输入)声明这些变量,并使用jQuery从输入框中获取所有这些变量。然后它会对它们进行seralize并将它们作为对象通过$_POST[]发送到PHP;如果使用此方法,jQuery将已经为您完成ajax工作。

Lets say this for an example:

举个例子:

 <form id="myForm">
 <button><input type="text" id="name" class="form-control" placeholder="Upisi ime"></button>
 <input type="hidden" id="presetName" name="javascriptVar">
 </form>
 <div id="result"></div>

and in your jQuery:

在您的jQuery:

$('#myForm').submit( function() {
var name = "john";

$('#presetName').val(name);  // inserting javascript's value
 var senddata = ""; // grabbing all inputs from the form
 senddata = $( this ).serialize(); // packing inputs up
 $.ajax({                                      
    url: 'yourPHP.php',                
   type: 'POST',
   data: senddata,
   success: function(data)   
  {
$('#result').html('<p> Data sent:'+data+'</p>');
      } //end of success
   }); //end of ajax
 event.preventDefault();
});// end submit

#2


1  

Use a hidden field inside your html form:

在html表单中使用隐藏字段:

<input type="hidden" name="theHiddenName" value="john">

#3


0  

If I'm understanding you right, you'll have to use either an AJAX or submit the form using a javascript post.

如果我理解正确,您将不得不使用AJAX或使用javascript post提交表单。

$(document).ready(function(){
    $('#name').change(function(){
        $.ajax({
            url: "submit.php",
            type: "post",
            data: 'name='+ $('#name').val()
        });
    });
});

http://jsfiddle.net/Wc9W2/

http://jsfiddle.net/Wc9W2/

Be sure to click "Run" first. Enter a name and then click off the input field so the change event will fire.

一定要先点击“运行”。输入一个名称,然后单击输入字段,以便更改事件触发。

#1


0  

You can declare these variables via input box (even hidden input) and using jQuery to grab all of these variables from the input boxes. It'll then seralize them and send them as an object to PHP via $_POST[]; If using this method, jQuery will be already doing the ajax work for you.

您可以通过输入框(甚至是隐藏的输入)声明这些变量,并使用jQuery从输入框中获取所有这些变量。然后它会对它们进行seralize并将它们作为对象通过$_POST[]发送到PHP;如果使用此方法,jQuery将已经为您完成ajax工作。

Lets say this for an example:

举个例子:

 <form id="myForm">
 <button><input type="text" id="name" class="form-control" placeholder="Upisi ime"></button>
 <input type="hidden" id="presetName" name="javascriptVar">
 </form>
 <div id="result"></div>

and in your jQuery:

在您的jQuery:

$('#myForm').submit( function() {
var name = "john";

$('#presetName').val(name);  // inserting javascript's value
 var senddata = ""; // grabbing all inputs from the form
 senddata = $( this ).serialize(); // packing inputs up
 $.ajax({                                      
    url: 'yourPHP.php',                
   type: 'POST',
   data: senddata,
   success: function(data)   
  {
$('#result').html('<p> Data sent:'+data+'</p>');
      } //end of success
   }); //end of ajax
 event.preventDefault();
});// end submit

#2


1  

Use a hidden field inside your html form:

在html表单中使用隐藏字段:

<input type="hidden" name="theHiddenName" value="john">

#3


0  

If I'm understanding you right, you'll have to use either an AJAX or submit the form using a javascript post.

如果我理解正确,您将不得不使用AJAX或使用javascript post提交表单。

$(document).ready(function(){
    $('#name').change(function(){
        $.ajax({
            url: "submit.php",
            type: "post",
            data: 'name='+ $('#name').val()
        });
    });
});

http://jsfiddle.net/Wc9W2/

http://jsfiddle.net/Wc9W2/

Be sure to click "Run" first. Enter a name and then click off the input field so the change event will fire.

一定要先点击“运行”。输入一个名称,然后单击输入字段,以便更改事件触发。