Kilisi
Kilisi

Reputation: 444

Passing value through .js to php, then using in SQL statement

I have a form, which is supposed to pass three values via .js to a .php page. First two values are for date range and work fine, but I'm lost on the syntax to pass the third value which is a name.

Then assuming I can get that working, the .php page needs to use that name in an SQL statement and again I'm unsure on the syntax, so assuming the value is "name" I have tried various variations on:-

This is the JS Function, which works ok for the daterange.

    function clickhere()
{
if (checkEmpty("dr_date")) return;
if (checkEmpty("end_date")) return;
if (checkEmpty("name")) return;

var d = convertDate(EL("dr_date").value);
var e = convertDate(EL("end_date").value);
var f = ("name").value;

if (d == null) {
    EL("dr_date").focus();
alert("The date must be in dd/mm/yyyy format.");
return;
}
if (e == null) {
    EL("end_date").focus();
alert("The date must be in dd/mm/yyyy format.");
return;
}

var x = getXmlHttpRequest();
if (x == null) {
    alert("Unable to get XmlHttpRequest");
    return;
}

//syntax problem here?

var u = "outlet-report-dr.php?dr_date=" + d + "&end_date=" + e + "&name=" + f; 

//syntax problem here?

x.open("GET", u, false);
x.send();

var t = x.responseText;
if (t != null && t != "") {
    var e = EL("content");
    e.innerHTML = t;
}

}

Then the PHP, first I get the values:-

    $dateStrt = $_GET['dr_date'];
    $dateFin = $_GET['end_date'];
    $name = $_GET['name'];

Then the SQL statement:-

    $sql = 'SELECT t.*
      FROM transfer t
     WHERE t.name = $name
       AND t.status <> \'cancel\'
     ORDER BY id DESC';

I have all the daterange part working nicely, but not the name, mostly I have just made a mess so far, please assist.

Upvotes: 0

Views: 50

Answers (1)

Swayze
Swayze

Reputation: 201

your get request should read like this name = value where each variable is separated by a &

eg "outlet-report-dr.php?dr_date=something&end_date=something&name=something

so you will need to change your var u = to read somthing like this

"outlet-report-dr.php?dr_date=" + d + "&end_date=" + e + "&name=" + name where name is the variable that holds your name value.

however if the name value you want is stored in a php varable? try something like this

"outlet-report-dr.php?dr_date=" + d + "&end_date=" + e + "&name=<?php echo $name;?>";

UPDATE

with you SQL... variables are appended to stings using a . in php

eg WHERE t.name = '.$name.'

Upvotes: 2

Related Questions