Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to use javascript inside PHP?

Featured Replies

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.

 

$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');
}

What do you expect to happen at if($result) ?

 

To be literal, if result...... what? I think your trying to check if the query is a success, in which case try this instead:

 

<?php
$qry = "insert into contactus(name,email,subject,message) values('$name','$email','$subject','$message')";
$result=mysql_query($qry);

// Query has failed, display message
if(!$result) { 
?>

<script type="text/javascript"> 
<!-- 
alert('Message failed');
//--> 
</script>

<?php
// Redirect to page
header('Location: contactus.php');
} else {
?>

<script type="text/javascript"> 
<!-- 
alert('Message sent');
//--> 
</script>

<?php
// Query has succeeded, redirect
header('Location: contactus.php'); }
?>

 

You should also in this day and age be validating input, i.e. protecting against SQL Injection and also using mysqli* instead of mysql*. That script/your database could be compromised in seconds.

 

Also check your original alert message for a failure and think about why that wouldn't work. I have not tested the above and it may not work, but mixing PHP (server-side) and JS (client side) is usually messy in this way. You're probably going to run into header errors as well I might add, I could solve them for you but where would be the fun in that :)

  • Author

The code doesnt work..

 

If($result) comes out to true, i want it to display an alert box and just 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.

Hi,

 

I never said it would work, I'm not writing it for you! It's just for ideas as what you had was wrong also.

 

If you want it fully diagnosed perhaps post your full code rather than just snippets.

 

Cheers

  • Author

I tried another method. It worked.

 

$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');  
}  

 

And this code in contactus.php

 

<?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>";  
}    
} ?>

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.