开发者

PHP jQuery AJAX

Trying to pass data to the server but it keeps returning a "Parameter Missing开发者_如何学Python"

So either the data is not being passed to the PHP script or I am doing something wrong.

Here is the jQuery:

function quickJob(obj) {

    var quickJobNumber = $(obj).text();
    //alert(quickJobNumber)
    $.ajax({
        type: "GET",
        url: "quickJobCB.php",
        data: quickJobNumber,
        success: function(server_response)
        {
            $("#message").removeClass().html(server_response);
        }
    });
}

Ok....when tracing the issue I created an alert as seen below. The alert is producing the expected results.

Here is the PHP script:

<?php

require_once("models/config.php");


// Make the connection:
$dbc = @mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if (!$dbc) {
    trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
}

if (isset($_GET['quickJobNumber'])) {
    $quickJobNumber = trim($_GET['quickJobNumber']);
    $quickJobNumber = mysqli_real_escape_string($dbc, $quickJobNumber);

    $query = "SELECT * FROM projects WHERE projectNumber = '" . $quickJobNumber . "'";
    $result = mysqli_query($dbc, $query);


    if ($result) {
        if (mysqli_affected_rows($dbc) != 0) {
            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

                echo $row['projectName'];
            }
        } else {
            echo 'No Results for :"' . $_GET['quickJobNumber'] . '"';
        }
    }
} else {
    echo 'Parameter Missing';
}
?>

<?php include("models/clean_up.php"); ?>


data: quickJobNumber,

should be

data: { 'quickJobNumber': quickJobNumber },


You'll need to pass the data either as a query string like so

data: "quickJobNumber="+quickJobNumber,

or a map like so

data: data { quickJobNumber: quickJobNumber },


If you want to use the GET request, use $.get

$.get("/get_request.php", { quickJobNumber: "myAjaxTestMessage"},
   function(data){
     console.log("WOW! Server was answer: " + data);
});

In php

<?php
if(isset($_GET['quickJobNumber'])){
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode(array('answer'=>'Hello user!'));
}
?>

If you want to use the POST request, use $.post

$.post("/post_request.php", { quickJobNumber: "myAjaxTestMessage"},
   function(data){
     console.log("WOW! Server was answer: " + data);
});

In php

<?php
if(isset($_POST['quickJobNumber'])){
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode(array('answer'=>'Hello user!'));
}
?>

P.S. or you can use $_REQUEST in php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜