Jump to content

Javascript form echo ?


queketth

Recommended Posts

Hi there

 

I am attempting to make a javascript form echo for a php form to replace the echo section of my script (which currently appears on a separate page with just plain text) I basically hope that i can have a little popout window that says "thankyou for contacting us!" or "please fill in the form with all the required details" etc replacing the current php echo...

 

Very unsure of how i would go about this and any help would be greatly appreciated...  :D

 

The code below is the current PHP

<?php

$name = $_POST['Name'];

$email = $_POST['Email'];

$telephone = $_POST['Telephone'];


$type = $_POST['Type'];

$message = $_POST['Message'];

$formcontent=" From: $name \n Type: $type \n Message: $message \n Telephone: $telephone";

$recipient = "[email protected]";

$subject = "Contact Form";

$mailheader = "From: $email \r\n";

if($name ==""  ||  $email  =="" || $type  ==""  ||$message  ==""  ){
echo"Fill the fields";
}

else{

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

echo "Thank You!";}

?>

Link to comment
https://forums.phpfreaks.com/topic/238589-javascript-form-echo/
Share on other sites

You shouldn't rely on JS for validation of your forms. By all means have both, to save the user submitting the form only to find out they have entered fields incorrectly. But you should always have the server-side validation there, as the user can disable and modify the client-side JS.

Look at other sites- a lot link to a separate page but still retain the header and footer. A very simple way to achieve that, is by placing your header and footer mark-up within two separate files and including them with PHP before and after your echos. Very simple example:

 

<?php require_once 'templates/header.php'; ?>

<h2>Example Page</h2>
<p>The header and footer are dynamically included into the page.</p>

<?php require_once 'templates/footer.php'; ?>

This approach also allows you to avoid duplicating the mark-up, so you don't have to make the same change across multiple pages. I don't know if you know much/any PHP, but it's not difficult to implement this, as you can see from the code above, to save yourself a lot of hassle and improve the consistency of your site's layout. You could also improve on it, using variables to dynamically set the title, description, etc.

 

Bottom line though, you shouldn't be resorting to JS in order to get around problems like this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.