Jump to content

Dynamic Values In Jquery Ajax


s4surbhi2218

Recommended Posts

Hi ,

m writing a code to add some dynamic values , following is my code which is currently hardcoded

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("Myfile.php",
{
 name:"Donald Duck",
 city:"Duckburg"
},
function(data,status){
 alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
<input type="text" id="txt1">
</body>
</html>

 

 

Now i want to send some value in city : entered from the text filed , i used

city :$('input[type=text]').val() but it did not work :( Please Suggest.

Thanks!!!

Edited by s4surbhi2218
Link to comment
Share on other sites

Try this:

 


$(document).ready(function(){
$("button").click(function(){
$.post("Myfile.php",
{
name: $('#txtName').val(),
city: $('#txtCity').val()
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});

 

I used the elements' ID attributes in the jQuery selector. You were selecting every text input field in your selector and trying to use the val() function on them. Instead, you need to select the specific element and grab its value. The HTML could look something like this:

 

<input type="text" name="txtName" id="txtName" />
<input type="text" name="txtCity" id="txtCity" />

Edited by Andy123
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.