1

Translate translator from Google. So that did not swear if something is not clear. Itself from Russia.

The question arose. How to pass the value of the alert in the javascript in the variable $ value in php, and write it in the case file. And another question: how to hide the alert? or use instead to visually it was not visible, but the value was passed?

    //a lot of code
{
     console.log(data);
     alert(data['value']);
    }
    });

So. Also there is a PHP script that writes logs (current page and the previous one) to a file. According to this principle here:

//a lot of code
$home = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$referer = $_SERVER['HTTP_REFERER'];
$value = how the value of the java script to convey here?;
$lines = file($file);
while(count($lines) > $sum) array_shift($lines);
$lines[] = $home."|".$referer."|".$value."|\r\n";
file_put_contents($file, $lines);

It is necessary that the value of js is transferred to the php-script and write to the file. How to do it? Prompt please. I am a novice in all of this.

4
  • 1
    did you consider trying to google - w3schools.com/php/php_ajax_database.asp Commented Oct 4, 2014 at 5:30
  • I really dont know what you mean by hiding thee alert -> maybe //alert() ? (comment the alert) Commented Oct 4, 2014 at 5:32
  • 1
    use AJAX to communicate from the client to the server Commented Oct 4, 2014 at 5:37
  • 1
    stackoverflow.com/questions/19462649/… Commented Oct 4, 2014 at 5:49

1 Answer 1

1

PHP scripts run before your javascript, which means that you can pass your php variables into javascript, but not the other way around. However, you can make an AJAX POST request from JavaScript to your PHP script, and grab the POST data in PHP through the global $_POST variable.

Assuming you use jQuery, your JavaScript would look something like:

// assign data object:
var data = { value: "test" };

// send it to your PHP script via AJAX POST request:
$.ajax({
    type: "POST",
    url: "http://your-site-url/script.php",
    data: data
});

and your PHP script would look like:

// if the value was received, assign it:
if(isset($_POST['value']))
    $value = $_POST['value'];
else
    // do something else;
Sign up to request clarification or add additional context in comments.

4 Comments

and without Ajax how to do it? naprmier can you? Just online java script code, and php code is also in the code and not in the file. I would like to optimize it all
You can not do it without AJAX, regardless of where your code is.
Clear. Thank you. That is more than any sort of no options? Only via ajax and post?
And if not difficult it can be written to a file? Give an example of the implementation of

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.