1

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 + <?);
3
  • JavaScript can't interact with PHP like this. PHP is serverside, while JS is clientside, so the only interactions would really be via AJAX. Commented Oct 4, 2012 at 8:49
  • when $result line is put on the codes win does not open and couldn't find the the win error when i write number instead of cuızgarakay.id it works correctly Commented Oct 4, 2012 at 8:49
  • @Blender I agree, HTTP POST/GET using AJAX would be the "standard way" of doing this. Commented Oct 4, 2012 at 8:49

2 Answers 2

4

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"
)
Sign up to request clarification or add additional context in comments.

6 Comments

thanks can you give any link including examples calling php file by sending data and getting data I tried some examples but it does not work
I'll make you a quick example of JS code making a HTTP request to a PHP page. Just give me a minute :)
how can i get the return value coming from php
thanks but :) my php url returns only one value and how can i keep iT ı guess that it changes there f(client.readyState == this.DONE) { if(client.status == 200 || client.status == 302 || client.status == 304) ok("POST","res","../phps/dbController.php"); I think res value must be from php is it true?
Yes, res will be a string containing the result from the server. Call ok like this ok(res).
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.