Calling javascript function from php code

Hi All,

I have problem in follwing script.

<?php
if(isset($_GET[‘submit’])){
if(!empty($_GET[‘name’])){
$name=$_GET[‘name’];

echo’<script type=“text/javascript”>show();</script>';

}
}

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
<title>Untitled Document</title>
<style type=“text/css”>
#display{ display:none; border:2px dashed #0033FF; width:200px}
</style>
<script type=“text/javascript”>
function show(){

document.getElementById(“display”).style.display=“block”;
}
</script>
</head>

<body>
<form method=“get” action=“form.php”>
<input type=“text” name=“name” />
<input type=“submit” name=“submit” />

</form>
<div id=“display”>

<?php echo $name; ?>
</div>
</body>
</html>

I am trying to make visible div when php code executes.

Can Anyone please help me on this???

Remove “display:none;” from the style, it is now visible. Otherwise you are looking in the wrong place PHP cannot execute Javascript, it is server-side.

Hi have purpose filly hided that div. because what i want is when that php code execute then only it should be visible, Like giving pop up kind of think???

PHP code and Javascript code do not execute at the same time.

PHP (Hypertext PREprocessor) runs on the server, before the client receives anything.
PHP finishes executing, and then the resulting HTML/javascript/etc code is sent to the client.
THEN the client’s browser runs the javascript.

What you’re probably looking to do here instead is an if(isset($_GET[‘name’]) { show form } else { show name }. Otherwise you’re getting into AJAX territory.

I want to display pop in php. Like whenever user fills the form and submit it then after Successful submission of form if the data is inserted in database the it should pop up like Thanks for contacting this is ur enquiry number…blah blah…

Can anyone please help me with this that how can i show this pop or only div after successful insertion of data from php.

use ob_start() before the echo of the js code and ob_end_flush(); after the echo.

I didn’t got what u r saying can plz edit the above code and post it back so that i can understand better way…

Hi,

Using JavaScript you can communicate between PHP and JavaScript using AJAX calls. This is a very basic example using the jQuery library as it can simplify the use of AJAX calls.


$.post(
    "./some_page.php"
    , {first_name: $("#first_name").val();  
    , last_name: last_val 
}
            , function(data){
    /* If data comes back from the PHP page this is where you get it. 
       You can return any type of data. More complex data you     
        may consider converting arrays to JSON, 
       but this example using simple text name values */

    var name =  firs_name + ' ' + last_name;
    if(data == name){
         var error = name+ ' ' + 'already exists. It was not added.';
         alert(error);
          $("#table_data").html('<p>' + error +'</p>'); 
          return 0;
     }
     /* If no errors then finish with these JavaScript functions*/
     displayMessage(data);
     setTimeout(function(){hideMessage();}, 4000);
     search(add_set);
  });

Now in your PHP page that gets called by AJAX, you handle the $_POST values like you would any other time. However you will notice that I don’t (in this example) return values instead I echo the finished presentation that the JavaScript displays.


<?php
/* some_page.php */
if($_POST['first_name'] && $_POST['last_name']){
  // Do your php work like SELECT, INSERT, or UPDATE you database with the post values that came from the AJAX call
  ...
  ...
  if($success){
     echo $name;
  } else {
    echo 0;
  }
}