0

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

5
  • what do you mean when you say, " but if I send a string everything works fine." ?? Commented Jan 31, 2011 at 16:31
  • Are you sure that input field is the only one on the DOM with id="name"? Commented Jan 31, 2011 at 16:40
  • @tsegay if I send in $.post-> name: 'My name', email: 'My email' instead of name: $('#name').val() Commented Jan 31, 2011 at 16:42
  • @lan Sure. I have tried $('input[name="name"]').val() too with the same result. Commented Jan 31, 2011 at 16:45
  • Updated my Answer and see if that can help. On my system with same configuration works fine. Commented Jan 31, 2011 at 16:49

1 Answer 1

1

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);
         });
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Aren´t you mixing JS and PHP sintax?. I have tried what you suggest with: var name = $('#name').val(); But it didn´t work
You are right. Your code work fine for me too. I found my error. I´m going to update my question. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.