简体   繁体   中英

PHP Form Works Correctly in Firefox, but not in IE and Chrome

I'm trying to create a form that will send to my email. The form works perfectly in Firefox, however, it does not work in Chrome/Internet Explorer . Hopefully someone can help!

EDIT What's happening is that my php is simply being displayed as text in the browser EDIT

HTML CODE:

   <form action="contact.php" method="post" id="contactform">
       <label for="myName">Name: </label>
       <input type="text" name="myName" id="myName" />

       <label for="myEmail">E-mail: </label>
       <input type="email" name="myEmail" id="myEmail" />

       <label for="myNumber">Phone Number: </label>
       <input type="tel" name="myNumber" id="myNumber" />

       <label for="myComments">Comments: </label>
       <textarea name="myComments" id="myComments" rows="2" cols="20"></textarea>

       <input id="mySubmit" type="submit" value="Submit">
   </form>

PHP:

<?php
$field_name = $_POST['myName'];
$field_email = $_POST['myEmail'];
$field_phone = $_POST['myNumber'];
$field_message = $_POST['myComments'];

$mail_to = 'alexandriadrugs@yahoo.com';
$subject = 'Message from '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'contact.html';
    </script>
<?php
}
else { ?>
        <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to alexandriadrugs@yahoo.com');
        window.location = 'contact.html';
    </script>
<?php
}
?>`

Firefox will run Php from the file directory while IE and Chrome will display them as plain text unless you specically tell them to run the file as the HTTP protocol. This may seem like a bug but they are infact doing a better job than Firfox by following the correct protocols being asked for:

OPEN FILE: file:///C:/xampp/htdocs/mypage.php

Request Webpage http:// localhost/mypage.php

I'm pretty sure that you're not running a web server that supports PHP. The PHP code won't be recognised that way, and it will be displayed like it's html.

  1. Go to google and type XAMP download.
  2. Download and install the application
  3. Go on C:/XAMP/htdocs and empty that folder
  4. PASTE the files that your friend gave to you righ there
  5. Make sure your file containing the form code that you posted here (on stackoverflow) is named index.html or index.php
  6. Go on XAMP control panel and start apache
  7. Open some web browser and type http:// localhost

You should have your problem solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM