Solved

How to prevent PHP from creating blank pages

Posted on 2009-05-13
3
316 Views
Last Modified: 2012-05-06
Hi,

On my website I load xml file using ajax. One of these files contains a mailform. In the action attribute of the form I vall upon a php-script. The only function it has, is send out an email if the form fields all validate. It does not have to echo back any result messages or confirmation messages. It all would work nicely if not for the anoying blank page that is opened upon pressing the submit button.

So, how do I prevent php (or is it the script?) from opening a new webpage?

<?php
// your name
$recipientname = "<myname>";
 
// your email
$recipientemail = "<my email>";
 
// subject of the email sent to you
$subject = "<some subject>";
 
// send an autoresponse to the user?
$autoresponse = "yes";
 
// subject of autoresponse
$autosubject = "<some subject>";
 
// autoresponse message
$automessage = "<some message>.";
 
// thankyou displayed after the user clicks "submit"
$thanks = "<some message>";
 
$message = 'Aanmelding voor ' . $_POST['TypeAanmelding'];
$formtype = $_POST['TypeAanmelding'];
$author = $_POST['Voornaam'] . ' ' . $_POST['Achternaam'];
$age = $_POST['Leeftijd'];
$address = $_POST['Adres'];
$city = $_POST['PcPlaats'];
$phone = $_POST['Telefoon'];
$email = $_POST['Email'];
$free = $_POST['Vakantie'];
$destination = $_POST['Bestemming'];
$comment = $_POST['Opmerkingen'];
$subject = $author . $_POST['Onderwerp'] . $formtype;
$verify = $_COOKIE['rndImgCode'];
$check = md5($_POST['SafetyCode']);
 
$autism  = $_POST['Autisme'];
if ($autisme == "true") $autism = 'Ja - ' . $_POST['SoortAutisme'];
else $autism = "Nee";
 
$school = $_POST['School'];
if ($school == "true") $school = 'Ja - ' . $_POST['SoortSchool'];
else $school = "Nee";
 
$job = $_POST['Werk'];
if ($job == "true") $job = 'Ja - ' . $_POST['SoortWerk'];
else $job = "Nee";
 
$occupation = $_POST['Dagbesteding'];
if ($occupation == "true") $occupation = 'Ja - ' . $_POST['SoortDagbesteding'];
else $occupation = "Nee";
 
$pgb = $_POST['PGB'];
if ($pgb == "false") {
  $pgb = "Nee";
  if ($pgbhelp == "true") {
    $pgbhelp = "- Ik wil graag hulp bij aanvraag PGB.";
  } else {
    $pgbhelp = "- Ik heb geen hulp nodig bij de aanvraag PGB.";
  }
} else {
  $pgbhelp = "";
} else $pgb = "Ja";
 
echo "$verify
$check";
 
if ($verify != $check)  exit();
$message .= "
 
Naam: $author
Leeftijd: $age
Adres: $address
Postcode/Plaats: $city
Telefoon: $phone
E-mailadres: $email
Autisme: $autism
School: $school
Werk: $job
Dagbesteding: $occupation
PGB: $pgb $pgbhelp
 
Mijn voorkeur gaat uit naar een $free
vakantie in het $destination.
 
Aanvullende opmerkingen:
$comment
";
 
$header = "From: $author <$email>";
mail($recipientemail,"$subject","$message","$header");
 
exit();
?>

Open in new window

0
Comment
Question by:CvD
3 Comments
 
LVL 8

Expert Comment

by:mostart
ID: 24376457
Dag,

you can't actually prevent it from opening the page at least not when using the post method.

I see two solution:

1.) you submit the page as you're doing it now and add a redirection to you mailscript
-> header('Location: http://www.thepagebefore.nl');

2.) you collect your data using javascript and do a GET request through ajax with the variables along. This is probably more elegant, but surely more work.
0
 
LVL 4

Accepted Solution

by:
Fugas earned 125 total points
ID: 24382500
If you're using prototype.js to Ajax calls do not submit the form, but make another Ajax call, like this example. If you have another functions to make Ajax calls, the syntax will be other.
<form id="your_form_id" onsubmit="new Ajax.Updater('your_div_id', 'script_where_pass_values', {method:post, asynchronous:true, evalScripts:true, parameters: $('your_form_id').serialize() }); return false"  action="#">

Open in new window

0
 
LVL 3

Author Closing Comment

by:CvD
ID: 31581048
I see what your getting at. I didn't know it could be done that way. So what I'll do is using the onsubmit event to post the form and pass a url to a php-script that will deal with the form content and send an email. And the output from the php-script will be intercepted using ajax again.
Thanks for pointing me in the right direction.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hbo knew my windows software 4 62
Magento: different theme for Pc and for mobile devices 4 31
Link failure 16 32
if (is_singular not working 5 18
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question