SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to use javascript inside PHP?

    I need to show an alert box to the user in case a form gets successfully submitted.

    But it is not working. The alert box does'nt pop up.

    The form is on page contactus.php. I want the user to stay on that page only. So i have added

    header('Location: contactus.php');

    Here is the code.

    PHP Code:
    $qry "insert into contactus(name,email,subject,message) values('$name','$email','$subject','$message')";
    $result=mysql_query($qry);
    if(
    $result)
    {

    echo 
    "<script type='text/javascript'>\n";
    echo 
    "alert('Your message was successfully sent.');\n";
    echo 
    "</script>";
    header('Location: contactus.php');
    }
    else
    {
    echo 
    "<script type='text/javascript'>\n";
    echo 
    "alert('Could'nt send your message.');\n";
    echo 
    "</script>";
    header('Location: contactus.php');


  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    remove the header() and it will work

  3. #3
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah but i want it to stay on the same page.

    It goes to contact_us_action.php which is the php handling the form. I want it to just stay on the same page. That is contactus.php

  4. #4
    SitePoint Enthusiast NuttySkunk's Avatar
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In that case you need to echo the JavaScript on the final page as currently (as suggested above) the page is redirecting before the JavaScript is seen by the user.

    You need to send back a message to contactus.php which allows you to tell the difference between a successful and failed message, you can do this with a query string (?name=value).

    PHP Code:
    $qry "insert into contactus(name,email,subject,message) values('$name','$email','$subject','$message')"
    $result=mysql_query($qry); 
    if(
    $result){ 
         
    header('Location: contactus.php?status=sent'); 
    }else{ 
         
    header('Location: contactus.php?status=fail'); 

    Add this to the top of contactus.php

    PHP Code:
    if(!empty($_GET['status'])){
         
    $status $_GET['status'];
         echo 
    '<script type="text/javascript">';
         echo 
    'alert("';
         echo (
    $status == sent) ? 'Your message was successfully sent.':'Could\'nt send your message.';
         echo 
    '");';
         echo 
    '</script>';


  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Then you must place the javascript code on contactus.php

    Alternatively you can use an AJAX request to send the data to contact_us_action.php and put the alert() code after the AJAX request was succesfully completed.
    However. I must tell you that users generally dislike the little popup boxes on websites. It's better to create a different form of feedback.

  6. #6
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @NuttySkunk..

    Your idea worked. Thanks a lot

  7. #7
    SitePoint Enthusiast NuttySkunk's Avatar
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are very welcome.

  8. #8
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your script had some bug. I did'nt work. I used this one.

    PHP Code:
    <?php 
    if(!empty($_GET['status'])) 
    {  
      
    $status $_GET['status']; 
         if(
    $status=="sent"
         { 
         echo 
    "<script type='text/javascript'>\n";  
    echo 
    "alert('Your message was successfully sent.');\n";  
    echo 
    "</script>";  
    }  
    else  
    {  
    echo 
    "<script type='text/javascript'>\n";  
    echo 
    "alert('Could'nt send your message.');\n";  
    echo 
    "</script>";  
    }    
    ?>


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
  •