Reputation: 4740
I am able to post a string to the PHP file but when I try and select an input field within a form to post the value inside it to the PHP file it doesn't work. Am I selecting the value in the input field correctly?
.js
$.post("js/data.php", {postid: form.postid.value},
function(output){
$("#gotpostid").html(output).show();
}).fail(function(x,y,z){
$("#gotpostid").html(x + "<br />" + y + "<br />" + z)
});
index.php
<div class="post">
<form name="form"><input type="text" name="postid" value="1234" />
</div>
If i simply do {postid: "some text"}
it works fine.
Upvotes: 1
Views: 153
Reputation:
try these.
$.post("js/data.php", {postid: document.getElementById("postid").value},
or
$.post("js/data.php", {postid: $("input[name='postid']")[0].value},
Upvotes: 1
Reputation: 91
you could try to send it like this:
$.post("js/data.php", $(this).serialize(),
function(output){
$("#gotpostid").html(output).show();
}).fail(function(x,y,z){
$("#gotpostid").html(x + "<br />" + y + "<br />" + z)
});
that way you send all the values from your form to the php and don't have to worry about it anymore
Upvotes: 0
Reputation: 1345
Use
$('input[name="postid"]').val();
instead of
form.postid.value
Upvotes: 1
Reputation: 391
Try the following:
<input type="text" name="postid" id="postid" value="1234" />
and grab the value by doing
value of postid is $("#postid").val() instead form.postid.value
Upvotes: 1