Web Design Forum: How to use javascript inside PHP? - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to use javascript inside PHP? Rate Topic: -----

#1 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 01 February 2012 - 05:02 AM

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

0

#2 User is offline   AdvantageDigitalMedia 

  • Forum Newcomer
  • Group: Gold Membership
  • Posts: 21
  • Joined: 31-January 12
  • Reputation: 1

Posted 01 February 2012 - 07:13 AM

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 :)
0

#3 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 01 February 2012 - 08:24 AM

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.
0

#4 User is offline   AdvantageDigitalMedia 

  • Forum Newcomer
  • Group: Gold Membership
  • Posts: 21
  • Joined: 31-January 12
  • Reputation: 1

Posted 01 February 2012 - 08:35 AM

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
0

#5 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 01 February 2012 - 12:27 PM

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

0

#6 User is offline   AdvantageDigitalMedia 

  • Forum Newcomer
  • Group: Gold Membership
  • Posts: 21
  • Joined: 31-January 12
  • Reputation: 1

Posted 01 February 2012 - 06:54 PM

You still should be validating data especially when using a database. Look into mysqli_real_escape_string(); and seriously look into mysqli* instead of mysql*.

Cheers
0

#7 User is offline   Kanishk Dudeja 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 30-November 11
  • Reputation: 0

Posted 02 February 2012 - 05:09 AM

Okay. Sure. Will look into it. Thank you :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users