dongzhong5967
dongzhong5967
2011-07-22 07:15
浏览 57

从变量中获取值,发送到php进行处理并将该值添加到mysql数据库中?

i am new to php and mysql.

How can i extract a VALUE from a JAVASCRIPT VARIABLE(i set) then send it to a PHP page that can read it and process it , the PHP will then insert the value into a table in MySQL database.

var A = "somevalue"

I have been researching but none of it give me a simple and direct answer . I saw some people uses JSON(which i am unfamiliar with) to do this.

Hopes someone can give me an example of the javascript/jquery , php code to this. Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanraa1984
    duanraa1984 2011-07-22 07:17
    已采纳

    You've asked for... a lot. But, this tutorial looks like it could help you.

    (FYI -- I swapped out the original tutorial for one on ibm.com. It's better but far more wordy. The original tutorial can be found here)

    点赞 评论
  • duanhuihui2705
    duanhuihui2705 2011-07-22 07:35

    What you need is Ajax. This is an example jQuery to use:

     function sendData(data) {
        $.ajax({ 
            type: 'POST',
            data: data,
            url: "/some/url/which/gets/posts",
            success: function(data) {
    
            }
        });
     }
    

    This will send post data to that url, in which you can use PHP to handle post data. Just like in forms.

    If you have a form:

    <form id="theformid">
       <input type="text">
    </form>
    

    Then you can use jQuery to send the form submit data to that sendData function which then forwards it to the other page to handle. The return false stops the real form from submitting:

    $("#theformid").submit(function(){
        sendData($(this).serializeArray());
        return false;
    });
    

    If you though want to send just a variable, you need to do it like this:

    function sendData(data) {
            $.ajax({ 
                type: 'POST',
                data: {somekey: data},
                url: "/some/url/which/gets/posts",
                success: function(data) {
    
                }
            });
         }
    

    Then when you are reading $_POST variable in PHP, you can read that data from $_POST['somekey'].

    Inside the success callback function you can do something with the data that the page returns. The whole data that the page returns is in the data variable for you to use. You can use this for example to check whether the ajax call was valid or not or if you need to something specific with that return data then you can do that aswell.

    点赞 评论
  • dongyaoxiu6244
    dongyaoxiu6244 2011-07-22 07:35

    I'm not pretty sure if it works but just try this. Your jQuery script shoul be like this:

    $(function(){
       var hello = "HELLO";
    
       $.post(
                "posthere.php",
                {varhello: hello},
                function(response){ alert(response); }
       )
    });
    

    and "posthere.php" is like this:

    $varhello = $_POST['varhello'];
    echo $varhello . ' is posted!';
    

    you should then get an alert box saying "HELLO is posted!"

    点赞 评论

相关推荐