Jump to content

pass variable in ajax


rdkurth

Recommended Posts

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

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);
        }
    });	

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.