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!!!

Link to comment
https://forums.phpfreaks.com/topic/272249-dynamic-values-in-jquery-ajax/
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" />

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.