Jump to content

Posting AJAX variables to PHP script


KevMart

Recommended Posts

Hi

 

This should be pretty simple since I have seen a lot of sites doing this.

 

Can someone see where I am going wrong for posting AJAX variables to a PHP script?  I think there is something wrong in the way I am passing the variables to PHP because when the AJAX variables are passed to the process.php script - and I try get them via the $_POST array - the $_POST array's count is 0 (i.e. zero, and I have tested this a number of times), so the die() command is always run.  How can I correctly pass the variables from javascript to PHP?  I am using jQuery !!!!

 

NOTE that I have tested that the AJAX script does retrieve the correct values because an alert command has proved that.

 

Below you will find some sample code for the various files.

 

 

index.php

<html>
<body>
<form method="post" name="searchform">
<table width="100%">
    <tr>
        <td>Select a table</td>
        <td><input type="text" name="table" id="table" /></td>
    </tr>
    <tr>
        <td>Enter username</td>
        <td><input type="text" name="username" id="username" /></td>
    </tr>
    <tr>
        <td><a href="javascript:void(null);" id="searchbutton">Search</a></td>
    </tr>
</table>
</form>
</body>
</html>

 

AJAX sample code

<script type="text/javascript">
function actionOnUser(php_script_url,adminaction,uname) {
    // create new script element, set its relative URL, and load it
    script = document.createElement( 'script' );
    script.src = php_script_url+adminaction+".php";
    document.getElementsByTagName( 'head' )[0].appendChild( script );
    alert(adminaction+" action on "+ uname +" still needs to be coded by "+script.src);
}

$(document).ready(function() {
    $("#searchbutton").click( function (eventObject) {
        var uname = $("#username").val();
        var tablename = $("#table").val();

        if(tablename == "")
            alert("Please select a table!");
        else
        {
            var protocol = window.location.protocol;
            var host = window.location.hostname;
            var url = protocol+'//'+host+'/process.php';

            $.post(url,{nickname:uname,table:tablename},function(data) {
                var eval = $.evalJSON(data);
                $('#viewform').append(eval.result);
                $('#viewform').show();
            });
            return false;
        }
    });
    $('#cancelbutton').click( function() {
        $('#viewform').hide();
    });
});

 

 

process.php

<?php
if(count($_POST) == 0) die("post array is empty");

mysql_connect('localhost','user','password');
mysql_select_db('database');

function performAdminQuery($table,$uname)
{
    $qry = "SELECT * FROM $table";

    if($uname != "")
        return array(mysql_fetch_assoc(mysql_query($qry." WHERE nickname='$uname'")));

    $result = mysql_query($qry);
    $num_rows = mysql_num_rows($result);

    $info = array();
    for($i=0;$i<$num_rows;$i++)
    {
        $data = mysql_fetch_array($result);
        
        $info[$i]['nickname'] = $data['nickname'];
        $info[$i]['email'] = $data['email'];
        $info[$i]['firstname'] = $data['firstname'];
        $info[$i]['lastname'] = $data['lastname'];
        $info[$i]['gender'] = $data['gender'];
        $info[$i]['birth'] = $data['birth'];
        $info[$i]['time_stamp'] = $data['time_stamp'];
    }

    return $info;
}

$uname_array = json_decode($_POST['uname'],true);
$table_array = json_decode($_POST['tablename'],true);

$uname = $uname_array['nickname'];
$table = $table_array['table'];

$adminpanel = performAdminQuery($table,$uname);

$res["result"] = $adminpanel;
echo json_encode($res);
?>

Link to comment
Share on other sites

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.