0

I have a problem when I want to send input data via jQuery's $.post. I have this input:

<input id="mydata" value='<?php echo $data; ?>' >

and I want to send its data via jQuery's $.post() function.

This $.post function runs, but it does't send anything:

var the_data = $('#mydata').val();
//alert(the_data); has correct data!!
$.post("http://php/server", the_data , function(data){
 alert(data);
}, "html");

But this one sends data correctly:

$.post("http://php/server", <?php echo $data; ?> , function(data){}, "html");

I should use input for saving data and method 2 isn't suitable for me.

Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
asked Sep 4, 2011 at 16:07
2
  • What type of input is it? Text, Password, Radio, etc? Commented Sep 4, 2011 at 16:10
  • type is hidden, but data is text Commented Sep 4, 2011 at 16:13

1 Answer 1

1

The data needs to be an object.

var the_data = {
 mydata: $('#mydata').val()
};

You can access this in your PHP with $_POST['mydata'].

answered Sep 4, 2011 at 16:09
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! but a minor error: ...val() has no ";". thanks again!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.