rdkurth Posted January 26, 2014 Share Posted January 26, 2014 I need to pass a variable from html through ajax to php The following code is what I have contacttext is what fells in the data from ajax <select name="contact"type="text" class="form-control input-sm contacttext" id="idcontact"></select> $( document ).ready(function() { $.ajax({ //create an ajax request to load_page.php type: "GET", url: "php/contact.php", dataType: "html", //expect html to be returned success: function(response){ $(".contacttext").html(response); //alert(response); } }); }); $id is what I need to pass threw the ajax script <?php include("connect.php"); $pid = $_SESSION['profile']['id']; echo "<option value=''>SELECT A CONTACT</option>"; foreach($db->query("SELECT * FROM contact WHERE pid = '$pid' AND cid = '$id'") as $row) { echo "<option value=" . $row['cid'] . ">" .$row['contact'] . "</option>"; } Link to comment https://forums.phpfreaks.com/topic/285698-pass-variable-in-ajax/ Share on other sites More sharing options...
kicken Posted January 27, 2014 Share Posted January 27, 2014 The object you pass to $.ajax can contain a data key which is the data to be posted to the script. You need to set this to the value you need to pass. Such as: $.ajax({ //create an ajax request to load_page.php type: "GET", url: "php/contact.php", data: { id: $('#idcontact').val() }, dataType: "html", //expect html to be returned success: function(response){ $(".contacttext").html(response); //alert(response); } }); Link to comment https://forums.phpfreaks.com/topic/285698-pass-variable-in-ajax/#findComment-1466675 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.