SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post 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????

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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???

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use ob_start() before the echo of the js code and ob_end_flush(); after the echo.

  7. #7
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  8. #8
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    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.
    Code:
    $.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 Code:
    <?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;
      }
    }
    ictus==""


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •