简体   繁体   中英

how can i use php codes in javascript file while php is including the javascript variable?

cuIzgaraKay is a javascript code I want to use it in sql command as cuIzgaraKay.id(it gets the value correctly I checked it). However, I could not it. I googled it but I can't find what I search . How can i do it ? sorry for asking syntax error but I can't find it maybe ı have logical error

        if(cuIzgaraKay){



          <?php
              include_once('../phps/kutuphane/inc.php');


    $result = mysql_query("select id from bolge_db where parent_id="?>+ cuIzgaraKay.id + <?);

As far as I can tell, you're trying to execute an SQL query from the PHP page, using data from the Javascript code you're generating.

The Javascript is only evaluated on the client side of your page. That means that the server-side cannot know anything about the Javascript code.

What you need to do, is to generate the ID, and communicate it to the PHP backend, for example with a HTTP POST/GET request or something.

EDIT:

If you're new to Javascript, I'd suggest you look into the native JS object XMLHttpRequest. You use it like this:

/*
    params:
            url:     relative URL on host, e.g. '/users/morten/data'
            method:  GET, POST, PUT, DELETE, HEAD or OPTIONS
            data:    data to send with request, e.g. POST form-data
            ok_cb:   function to call on success
            fail_db: function to call on failure
*/
function http_req(url, method, data, ok_cb, fail_cb) {
    var client = new XMLHttpRequest();
    client.onreadystatechange = function () {
        if(client.readyState == this.DONE) {
            if(client.status == 200 || client.status == 302 || client.status == 304)
                ok_cb(...);
            else
                fail_cb(...);
        }
    };
    client.open(method, url, true);
    client.send(data);
}

Or you can use jQuery or a ton of other frameworks to do the same thing.

EDIT:

what is method in there how can i write there and how can i get data from php – user1702486

The method is whatever HTTP request method you want to use. See the wikipedia page for more info: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

This code will POST to /page.php sending a data-string along. If the request returns a successful code, the ok -callback will be called.

// handle HTTP OK
var ok = function(meth, res, url) {
    console.log("OK: got " + res);
}

// handle all else
var fail = function(meth, res, url) {
    console.log("UTTER UTTER FAILURE!\n" + res);
}

// make a HTTP POST to server/page.php sending the data along
// you can serialize the data and use XML or JSON or whatever instead
http_req("/page.php", "POST", "username=usr&password=passw&userid=12345", ok, fail);

On the PHP page, you will need to handle the POST by checking the data and returning an answer if appropriate. Something like this:

<?php
if (empty($_POST))
{
    print_r($_POST);
    ...
    // do something with the data and print something back
}
?>

If you've posted "data1=1234&data2=5678&str=hello%20mum" the $_POST array will look like this:

(
    [data1] => 1234
    [data2] => 5678
    [str] => "hello%20mum"
)

You can't, javascript is client side and php is server side. So the php code gets executed before the javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM