Jump to content

[SOLVED] Problem with PHP contact form


Joe Cool

Recommended Posts

Hi, a client has asked me to overhaul the SEO of his existing website and make one or two other changes.

 

As I was testing the site I noticed that the contact form on one of the sections doesn't work. When you click the submit button (labelled the 'Confirm Call Back' button) it doesn't have the desired effect. It should display a message on the screen confirming the information has been received and the info the visitor entered should be emailed to the client. Neither is happening. When the button is clicked the information disappears from the form, no message is displayed on the screen and no email is sent out.

 

There is a form checker in the code, which checks that a call back time, email and phone number has been entered. The form checker is working ok but that's it.

 

I should point out that I have no experience in PHP coding so I'm not sure what the problem is.

 

I've listed the original code below: The only things I've changed are the company name and the client's email address. Any help you could give me would be appreciated.

 

 
<?php
$title = "Mortgages - Company x - Local independant financial advisors";
    $keywords = "mortgage, mortgages, england, united kingdom, local, financial, advisors, re-mortgage, repayment mortgages, first time buyer mortgage, self cert mortgage, buy to let mortgage, interest only mortgages, commercial mortgage, let to buy mortgages, fast track mortgage, holiday let mortgage, self build mortgages, renovation mortgages, mortgages for property conversion";
    $description = "First time buyer mortgages, interest only mortgages and repayment mortgage offers and advice from local independant financial advisors from across England.";
// $footkey = "<strong>Company x mortgage services:</strong> 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"mortgages in england\">mortgages in england</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"mortgage quote\">mortgage quote</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"re-mortgage\">re-mortgage</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"repayment mortgage\">repayment mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"first time buyers mortgage\">first time buyers mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"self cert mortgage\">self cert mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"buy to let mortgage\">buy to let mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"interest only mortgage\">interest only mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"commercial mortgage\">commercial mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"let to buy mortgage\">let to buy mortgages</a>, 
    //  <a href=\"mortgages.php\" class=\"foot2\" title=\"fast track mortgage\">fast track mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"self build mortgage\">self build mortgages</a>, 
    // <a href=\"mortgages.php\" class=\"foot2\" title=\"renovation mortgage\">renovation mortgages</a> 
    // and <a href=\"mortgages.php\" class=\"foot2\" title=\"property conversion mortgages\">mortgages for property conversions</a>.";
    $page = "mortgages";
include ('includes/head.php');
    
    if ($button == "Confirm Call Back") {

$sendemail = "Name: $name
Email address: $email
Phone number: $phone
Mobile number: $mobphone
Town: $town
County: $county
Purchase price / Property value: £$propertyvalue
Mortgage type: $mortgagetype
Callback 1: $hours1:$minutes1 - $date1
Callback 2: $hours2:$minutes2 - $date2

Extra info: 
$info";
        $subject = "Mortgage call back request from company x";
        $mailheaders = "From:$email\n";
        $mailheaders .= "Reply-To:$email\n";
        $message="".stripslashes($message);
        $subject=stripslashes($subject);
        mail("[email protected]",$subject,$sendemail,$mailheaders);
        $status = "<span style=\"color:red;\"><strong>Your call back request has been sent successfully.</strong><br />Thank you for contacting us. We aim to get back to you as you have specified.</span>";
    
    }
?>


<h1>Company x Mortgages</h1>

<div class="maincont">

    <? if ($status) { print "$status<br /><br /><br />\n"; } ?>

    Local Independent Financial Advisors from across the UK.
    <br /><br /><br />
    <h2>Request a Call Back</h2>
    <br />
    Please request a time you would like an advisor to call. We aim to return calls within one hour.
    <br /><br />
    <script language="javascript" type="text/javascript">
function checkform() {
	if (document.callback.hours1.value == '') { alert('Please enter a call back time'); return false; } 
	else if (document.callback.minutes1.value == '') { alert('Please enter a call back time'); return false; }
	else if (document.callback.date1.value == '') { alert('Please enter a call back date'); return false; }
	else if (document.callback.email.value == '') { alert('Please enter your email address'); return false; }
	else if (document.callback.phone.value == '') { alert('Please enter your phone number'); return false; }
	return true;
}
</script>
    <form id="callback" name="callback" method="post" action="mortgages.php" style="display:inline;" onSubmit="return checkform()">
        Your first choice:<br />
        <script language="javascript" type="text/javascript" src="datetimepicker.js"></script>
        <select name="hours1" id="hours1">
            <option value="" selected>--</option>
            <option value="08">08</option>
            <option value="09">09</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
            <option value="16">16</option>
            <option value="17">17</option>
            <option value="18">18</option>
            <option value="19">19</option>
            <option value="20">20</option>
            <option value="21">21</option>
        </select>:<select name="minutes1" id="minutes1">
		<option value="" selected>--</option>
            <option value="00">00</option>
            <option value="05">05</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
            <option value="30">30</option>
            <option value="35">35</option>
            <option value="40">40</option>
            <option value="45">45</option>
            <option value="50">50</option>
            <option value="55">55</option>
        </select>
        <input name="date1" type="text" id="date1" size="10" /> 
        <a href="javascript:NewCal('date1','ddmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> *
        <br /><br />
        Your second choice:<br />
        <select name="hours2" id="hours2">
		<option value="--" selected>--</option>
            <option value="08">08</option>
            <option value="09">09</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
            <option value="16">16</option>
            <option value="17">17</option>
            <option value="18">18</option>
            <option value="19">19</option>
            <option value="20">20</option>
            <option value="21">21</option>
        </select>:<select name="minutes2" id="minutes2">
		<option value="--" selected>--</option>
            <option value="00">00</option>
            <option value="05">05</option>
            <option value="10">10</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="25">25</option>
            <option value="30">30</option>
            <option value="35">35</option>
            <option value="40">40</option>
            <option value="45">45</option>
            <option value="50">50</option>
            <option value="55">55</option>
        </select>
        <input name="date2" type="text" id="date2" size="10" /> 
        <a href="javascript:NewCal('date2','ddmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
        <br /><br />
        Your name:<br />
        <input name="name" type="text" id="name" size="30" />
        <br /><br />
        Your email address:<br />
        <input name="email" type="text" id="email" size="30" /> *
        <br /><br />
        Your home phone number:<br />
        <input name="phone" type="text" id="phone" size="30" /> *
        <br /><br />
        Your mobile phone number:<br />
        <input name="mobphone" type="text" id="mobphone" size="30" />
        <br /><br />
        Your town:<br />
        <input name="town" type="text" id="town" size="30" />
        <br /><br />
        Your county:<br />
        <input name="county" type="text" id="county" size="30" />
        <br /><br />
        Mortgage amount:<br />
        £ <input name="propertyvalue" type="text" id="propertyvalue" size="25" />
        <br /><br />
        Type of mortgage required:<br />
        <select name="mortgagetype" id="mortgagetype">
            <option selected>Select One</option>
            <option>-------------</option>
            <option value="Remortgage">Remortgage</option>
            <option value="Repayment">Repayment</option>
            <option value="First time buyer">First time buyer</option>
            <option value="Sert cert">Self cert</option>
            <option value="Buy to Let">Buy to Let</option>
            <option value="Interest Only">Interest Only</option>
            <option value="Commercial">Commercial</option>
            <option value="Let to Buy">Let to Buy</option>
            <option value="Fast Track">Fast Track</option>
            <option value="Holiday Let">Holiday Let</option>
            <option value="Self Build">Self Build</option>
            <option value="Renovation">Renovation</option>
            <option value="Property Conversion">Property Conversion</option>
        </select>
        <br /><br />
        Different requirements:<br />
        <span style="font-size:11px;">If the service your require is not on the drop down list above, please describe it in the box below:</span><br />
        <textarea name="info" cols="50" rows="5" id="info"></textarea>
        <br /><br />
        
        <input type="submit" name="button" id="button" value="Confirm Call Back" />
    </form>

    <br /><br />
    Required fields are marked with a *    
    
</div>

<?php
include ('includes/foot.php');
?>

Link to comment
https://forums.phpfreaks.com/topic/176675-solved-problem-with-php-contact-form/
Share on other sites

Hi Joe Cool,

 

None of the POSTed data is being populated into the relevant variables, e.g. $name, $email, $phone, $mobphone etc etc are defined but they are all NULL values.

 

You need to do something like:

 

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$mobphone = $_POST['mobphone'];
etc, etc

 

This will then populate the variables with the relevant data.

 

The if statement which decides whether or not the submit button is pressed also relies on the $button variable being set with "Confirm Call Back".  For better compatibility remove this line completely and amend with the following:

 

Remove:

 

if ($button == "Confirm Call Back") {

 

and replace with:

 

if($_SERVER['REQUEST_METHOD'] == 'POST') {

 

Hope this helps.

Hi

 

Looks like the page is quite old and was written assuming that register globals was set on:-

 

http://www.php.net/manual/en/security.globals.php

 

My guess is that you are testing on a different machine with a fresh and modern install of php, which will likely have register globals turned off. If this is the case then I suspect you will have the same problem elsewhere.

 

While you could turn it on I would recommend against it. However cleaning the code up could be a bit time consuming.

 

All the best

 

Keith

Thanks for your replies.

 

Apparently the code was written a few years ago. The client recently changed hosting companies, so their server may read the code differently. I had a problem with an asp contact form in the past for the same reason.

 

Bricktop, I replaced the button code with the following as you suggested:

 

if($_SERVER['REQUEST_METHOD'] == 'POST') {

 

The form submitted and displayed the confirmation message on the screen. An email was also sent but there was no content. They are NULL values as you said. I then entered your other suggested code:

 

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$mobphone = $_POST['mobphone'];
etc etc

 

Everything now works as it should. I noticed that none of the contact forms on the website are working, so it looks like I'll have to make similar adjustments to each form.

 

Thanks for your help, it's much appreciated.

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.