s4surbhi2218 Posted December 21, 2012 Share Posted December 21, 2012 (edited) 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 December 21, 2012 by s4surbhi2218 Quote Link to comment https://forums.phpfreaks.com/topic/272249-dynamic-values-in-jquery-ajax/ Share on other sites More sharing options...
Andy123 Posted December 21, 2012 Share Posted December 21, 2012 (edited) 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 December 21, 2012 by Andy123 Quote Link to comment https://forums.phpfreaks.com/topic/272249-dynamic-values-in-jquery-ajax/#findComment-1400752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.