m_gunns
m_gunns

Reputation: 547

PHP jQuery AJAX

Trying to pass data to the server but it keeps returning a "Parameter Missing"

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"); ?>

Upvotes: -2

Views: 100

Answers (3)

TROODON
TROODON

Reputation: 1175

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.

Upvotes: 0

JohnD
JohnD

Reputation: 4002

data: quickJobNumber,

should be

data: { 'quickJobNumber': quickJobNumber },

Upvotes: 3

Tomm
Tomm

Reputation: 2142

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 },

Upvotes: 0

Related Questions