Jump to content

Recommended Posts

Hello guys,

 

I was just wondering what I was doing wrong here, I've 'created' a website form in PHP but when I click my submit button it doesn't seem to fetch: 

 

php.php?s=1

 

or 

 

php.php?s=2

 

which is then used to print either Success or Failure.

 

Here's my code:

<html>
<head>

  <title></title>
  <meta charset="utf-8" /> 
  <link rel="stylesheet" href="footer.css" type="text/css"/>
  <link rel="stylesheet" href="header.css" type="text/css"/>
  <link rel="stylesheet" href="php/phpbody.css" type="text/css"/>

</head>

<!--body-->
<body>
	<div id="container">
		<h3 id="h3">Contact Me:</h3>
		<?php
			$s = isset($_GET['s']) ? $_GET['s'] : '';
			if ($s=="1") {
				echo ('<span class="success">Success! Your enquiry has been sent.</span>');
			}else if ($s=="2"){
				echo ('<span class="fail">Sorry! Your enquiry has not been sent. 
					Please ensure you have filled in the form correctly.</span>');
			}
		?>
		<h5 id="h5">To contact me, please use this form, created with PHP:</h5>
		<form id="form1" name="form1" method="POST" action"php/send.php">
			<strong>First Name:</strong><br><input type="text" name="firstname" id="firstname"><br><br>
			<strong>Last Name:</strong><br><input type="text" name="lastname" id="lastname"><br><br>
			<strong>E-mail:</strong><br><input type="text" name="email" id="email"><br><br>
			<strong>Enquiry:</strong><br><textarea name="enquiry" id="enquiry" cols="45" rows="5"></textarea><br><br>
			<strong>Security:</strong> 6 + 4 = <input type="text" name="security" id="security" size="1"><br><br>
			<input type="submit" name="submit" id="submit" value="Submit"/>
		</form>
	</div>	

  <?php include('header.php'); ?>
  <?php include('footer.php'); ?>

</body>
</html>

Send.php:

<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$enquiry = $_POST['enquiry'];
$security = $_POST['security'];

$to = "example@Outlook.com";
$subject= "New Form Enquiry";
$message = "A potential employer has submitted an enquiry from your website:\n\n 
First Name: $firstname\n\n Last Name: $lastname\n\n Email: $email \n\n Enquiry: $enquiry\n\n
You should probably respond.";

if($security = "10"){
	mail($to,$subject,$message);
	header("Location:php.php?s=1"); //php.php being the forms location
}else{
	header("Location:php.php?s=2"); //php.php being the forms location
}

?>

Any ideas would be much appreciated.

 

Thanks.

Edited by Coplestone
Link to comment
https://forums.phpfreaks.com/topic/294020-creating-a-website-form-in-php/
Share on other sites

if you use the POST method there will never be a GET generated unless you change the action tag to something like action="send.php?s=1". You could do this with javascript but it is unlogic.

 

I should do something like this:

<form action="" method="POST ">

 

the empty action tag will hold the user on one and the same page.

 

you can use $_SERVER['REQUEST_METHOD'] to test if the form has been posted.

 

after it is you can validate the form. if there is something wrong (like no email) then you just let the page load again (with an error message).

 

if everything is alright then you send your email and after that you can redirect the page to another page like 'thankyou.php' with this snippet

<?php

header('Location: thankyou.php');
exit;

?>

aware that there may not be any output before you send headers

I was just wondering what I was doing wrong here, I've 'created' a website form in PHP but when I click my submit button it doesn't seem to fetch: 

 

php.php?s=1

 

or 

 

php.php?s=2

 

Could you provide a little more information about what is happening? Does the send.php script redirect at all? Or is there a problem with your form recognizing $_GET['s']?

 

 

Note that you are using an assignment operator here:

 

if($security = "10"){

 

You'll need to use two equal signs to make a comparison.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.