0

Possible Duplicate:
How to call a JavaScript function from PHP?

I'm working on a project for school, and I'm struggling with the login page. In my PHP code, if the user enters an incorrect username or password, I want to call a Javascript function that displays a message and briefly changes the background colour where the message is shown. Here are my JS and PHP code blocks:

<script>
var flashContent = function () {
document.getElementById("outputlogin").style.backgroundColor = "#ffff00";
document.getElementById("outputlogin").innerHTML = "Incorrect login.";
function proxy() {
    updateColor(0);
}
setTimeout(proxy, 50);
}

var updateColor = function (newColor) {
var hexColor = newColor.toString(16);
if (hexColor.length < 2)
    hexColor = "0" + hexColor;
var colorString = "#ffff" + hexColor;

document.getElementById("outputlogin").style.backgroundColor = colorString;

function proxy() {
    updateColor(newColor);
}

if (newColor < 255) {
    newColor = newColor + 5;
    setTimeout(proxy, 50);
}
}
</script>

<?php
$username = $_POST['username'];
$password = $_POST['password'];

if(($username == "Ben") && ($password == "thepassword")){
//echo "SUCCESS";
session_start();
$_SESSION['bensedmgallerysesh'] = session_id();

header("Location:../index.php");

}else{
if($username != "" && $password != ""){
    javascript:flashContent();
}
}
?>

Right now, after hitting the login button, I get the error message:

Fatal error: Call to undefined function flashContent()

How do I fix this?

Community
  • 1
  • 1
Ben
  • 139
  • 6
  • 13
  • 2
    You also want to review [this question over on Meta](http://meta.stackexchange.com/a/5235/135887) about how that big fat orange "0%" under your name may be impacting your ability to get answers. – Charles Dec 11 '12 at 23:29
  • You're using Javascript as a client-side language and PHP as a server-side language. You can't directly call a method in one of these languages from the other. – Matt Huggins Dec 11 '12 at 23:30
  • do you understand know what JS and PHP operate into entirely completey different contexts? One is client side (browser), JS, and one is server side, PHP. If you need a PHP script to validate your form server side, then you have two options, make an AJAX request to the PHP script and have the JS act accordingly, or have the form submit to a PHP script and have it output some HTML/JS that will do what you need it do. There's no calling one from within the other. – thescientist Dec 11 '12 at 23:30
  • http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – Popnoodles Dec 11 '12 at 23:36

4 Answers4

2

You're trying to call a client side function in a server side script. You COULD do this:

if($username != "" && $password != ""){
?>
    <script type="text/javascript">
        flashContent();
    </script>
<?php 
}

But it might be smarter to actually separate the logic in a way that prevents you from trying to write server side processing like this. Organizational skills go a long way in this business.

Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
1

Have you tried doing this:

echo '<script type="text/javascript"> flashContent(); </script>';

Instead of:

javascript:flashContent();
Dim13i
  • 1,961
  • 1
  • 17
  • 20
0

I dont think there is such thing like javascript: functionName()..

Javascript runs in user space, interpreted by the browser.

PHP runs in server space, and is processed by a preprocessor.

What you should do there is test the username and password in PHP, and if they are wrong send back a JSON reply to the client. In the client, parse the JSON data and show the corresponding message. You may want to do this with AJAX.

Hope that works

chesscov77
  • 770
  • 8
  • 14
0

You cannot call a Javascript function like that with php, you would need to echo out the script calling the function in javascript. Something like this might work:

<?php
echo '<script> window.onload = function(){ flashContent(); } </script>';
?>

But that might not be a great way to do it. Php runs on the server before javascript runs on the browser. One better way to have the two languages communicate is to send AJAX calls to the php script.

EDIT To clarify: You can echo out a call to a Javascript function in the script, and it will be run where you echoed it. But here, php will still just treat Javascript as a string to be printed on the document.

Pjottur
  • 632
  • 9
  • 22