0

I'm making a website, and have been trying to write javascript in a PHP file to create an alert, then depending on if the user clicks "ok" or "cancel", it takes them to different pages. I got a javascript code that should work in an HTML file, but the PHP file doesn't read it for some reason.

This is for a XAMPP server, running MySQL, PHP, HTML, CSS, javascript, and Apache. I tried pasting this into my PHP file, but it doesn't read properly.

if ($sessioncode != "Admin-placebojoe" || "Mizzy-renegade") {
    loginFail();
} else {
    loginSuccess();
}

function loginFail() {
    var txt;
    var r = confirm("Account does not exist. Either check credentials and try again, or press ok to Sign Up.");
    if (r == true) {
        txt = header("refresh:6; url=Signup.html");
    } else {
        txt = header("refresh:0; url=Login.html");
    }
}

The PHP file rejects the line where it says var txt; on line 8. The error message reads:

Parse error: syntax error, unexpected 'var' (T_VAR) in C:\xampp***** on line 13

(I left out a few lines at the beginning that's why it's line 13). This tells me that the error is the PHP file cant read the JavaScript.

How can I make the PHP file read and execute all of the JavaScript code?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mizzy
  • 1
  • 1
  • Hi Mizzy welcome to SO. It seems you're using PHP code within Javascript. To change location in Javascript try using ```window.location = 'Login.html' ``` instead of ```header("refresh:0; url=Login.html"); ``` – Chif Oct 16 '19 at 21:58
  • You are mixing PHP and Javascript. They are 2 different things and run at two different places. PHP runs of server and spits out the html to browser, job done! Then Javascript runs on the browser inside html. – Nawed Khan Oct 16 '19 at 21:59
  • There are several issues in your code. One of them is right at the beginning: if ($sessioncode != "Admin-placebojoe" || "Mizzy-renegade") { What you probably meant is if (!in_array($sessioncode, array("Admin-placebojoe", "Mizzy-renegade"))) { which means "if sessioncode is **none** of the following...". What you wrote means: "If sessioncode is not Admin-placebojoe, OR (if) Mizzy-renegade..." And "Mizzy-renegade" is a string, so it means true. And anything OR'ed with a truthy value is true. So your code will always loginFail(). – LSerni Oct 16 '19 at 22:12
  • 1
    @LSerni except that, as stated in the post, the code is (intended as) JavaScript and in_array is a PHP function. I do agree that the logic is wrong. However that's not the fundamental issue here. – ADyson Oct 16 '19 at 22:13
  • Thank you everyone! I had no idea people answered so quickly on this website. I thought it would take weeks for anyone to even see the question! And thank you to LSerni for the information, I didn't know that my code was incorrect. I only started learning php one day ago. Thank you all again! – Mizzy Oct 16 '19 at 23:58

1 Answers1

2

PHP doesn't read or execute JavaScript. JavaScript runs in the browser.

If you include a JavaScript block in your .php file, PHP will (just as it would if you include a raw HTML or CSS block in a .php file) simply pass that code as-is to the browser in the response it gives to the HTTP request coming from that browser.

However if you are getting the error mentioned it implies that you tried to put this code inside a <?php block. This means the PHP interpreter will try to execute it as PHP code on the server. But as you said yourself: it isn't PHP code, it's JavaScript. Therefore (obviously I hope) the PHP interpreter cannot understand it and you get a syntax error.

If you want to include some JavaScript code to run in your browser, then move it outside the <?php ... ?> tags and put it inside a <script> block instead, e.g.

<script>
  alert("Hello, World");
</script>

P.S. For background you may want to read this popular and informative thread: What is the difference between client=side and server-side programming?

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/200989/discussion-on-answer-by-adyson-why-does-the-php-file-not-read-the-javascript-pro). – Samuel Liew Oct 17 '19 at 01:41