开发者

Problem in jQuery sending values with $.post

EDITED: After two hours trying to solve my problem I was obfuscated, so I came to StackOverflow, I read related questions that did´nt solve my problem, and finally I asked help. I simplified my code and posted this question. Testing the code you gave me I could see where I was failing. That's embarrassing: just before calling $.post I was changing the content of the form div to show an ajax loader gif, so when $.post was called the inputs had been deleted.

Hello. I´m testing jQuery AJAX functions, and I can´t send data by $.post. This is the process:

  • form.php: a html form and a jquery script that send values by $.post and alerts the result
  • process.php: a php script that receives the values, work with them and returns something.

FORM

<form action="#" method="post">
<label for="name">Your name</label>
<input type="text" name="name" id="name" />
<label for="email">Your email</label>
<input type="text" name="email" id="email" />
<input type="button" value="send" id="btnSend" />
</form>

JQUERY开发者_运维问答

    function sendAjaxPost(){

        $.post('process.php',{
                name: $('#name').val(),
         email: $('#email').val()
         },
         function(data){
           alert(data);
         });
        }

$('#btnSend').click(function(){
   sendAjaxPost();
}

PROCESS.PHP

foreach($_POST as $key => $value)
{
 echo $key . ' => ' . $value . '<br/>';
}

I can´t send $('#name').val(), but if I send a string everything works fine. I have alert $('#name').val() inside the jQuery function and it´s possible to read it. I have followed the sintaxis shown in $.post.

Could someone give me a light? Thanks in advance


If you can read the input fields on the function, you can try this,

function sendAjaxPost(){
         name=$('#name').val();
         email=$('#email').val();
         alert(name);   
         alert(email);   
        //If this two alerts the name and email, try with $.get();
        $.post('process.php',{
                name: name,
                email:email
         },
         function(data){
           alert(data);
         });
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜