Jump to content

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