Hello bretheren and programming Gurus? Am new and blank about php and just trying to experiment to get things as i want. Am using the code below but it does not validate data input and also does not redirect to the index page specified in it.I want if the fileds are empty on clicking submit, it must return an error and prompts to fill in all fields.i also want an option where a person can attach files to the form. Kindly correct errors for me in the code and give me a complete fully functional code. Thank you.

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

if(isset($_POST['submit']))

{
    $from_add = "contactform@yourwebsite.com";

    $to_add = "yourname@yourwebsite.com";

    $subject = "Your Subject Name";

    $message = "Name:$name \n Email: $email \n Phone: $phone \n
Message: $message";

    $headers = "From: $from_add \r\n";
    $headers .= "Reply-To: $from_add \r\n";
    $headers .= "Return-Path: $from_add\r\n";
    $headers .= "X-Mailer: PHP \r\n";


    if(mail($to_add,$subject,$message,$headers))
    {
        $msg = "Mail sent";
    }
}

    print "<p>Thank you $name for your message,
we will be in contact shortly. <a href=\"index.php\">Click here</a>
to continue </p>" ;



?>

Recommended Answers

All 5 Replies

You want to redirect automatically ?

i think you have to change the link in your <a> tag but if you want to redirect automatically, you have to user header() function in php .

Hi for client side you can add Javascript for form validation. when the submit button is click it will check your form element if its is empty or not..or if your using html5 just use the new required attribute and for a server side validation use (PHP) just to be sure.

for html5
<input type="text" name="name" required>

for the JS Part something like this

<script language="javascript">
function validate()
{
var x=document.forms["formname"]["name"].value;
if (x==null || x=="" )
  {
  alert("Name is Required");
  return false;
  }
  var x=document.forms["formname"]["email"].value;
else if (x==null || x=="" )
  {
  alert("email is Required");
  return false;
  }
var x=document.forms["formname"]["phone"].value;
else if (x==null || x=="" )
  {
  alert("phone is Required");
  return false;
  }
 var x=document.forms["formname"]["message"].value;
else if (x==null || x=="" )
  {
  alert("message is Required");
  return false;
  }


}
</script>

and for the php validation part just convert it to php:D

usually when you want to redirect to a page automatically you just need to put a header tag after your email been send like

header("Location:index.php");

ok i tried both your suggestions. But the link with file upload appears to appeal more of my desire. thanks to everyone. but one problem. when i hit submit button i get php gabbage on the screen rather than the supposed after send button message. Check it below. i have provided both php file and the html form codes. Any idea how these insects like characters come about? and where do i put my own e-mail address in the php code to which all e-mails can be send to?

Have a look

This PHP gabbage is displayed on the web page when i hit submit

\nReply-To: ".$_POST["txtFormEmail"].""; $strHeader .= "MIME-Version: 1.0\n"; $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n"; $strHeader .= "This is a multi-part message in MIME format.\n"; $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-type: text/html; charset=utf-8\n"; $strHeader .= "Content-Transfer-Encoding: 7bit\n\n"; $strHeader .= $strMessage."\n\n"; //*** Attachment ***// if($_FILES["fileAttach"]["name"] != "") { $strFilesName = $_FILES["fileAttach"]["name"]; $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; $strHeader .= "Content-Transfer-Encoding: base64\n"; $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n"; $strHeader .= $strContent."\n\n"; } $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error // if($flgSend) { echo "Mail successfully sent."; } else { echo "Cannot send mail."; } ?>

Html form code

<form action="php_sendmail_upload2.php" method="post" name="form1" enctype="multipart/form-data">  

<table width="343" border="1">
<tr>
<td>To</td>
<td><input name="txtTo" type="text" id="txtTo"></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="txtSubject" type="text" id="txtSubject"></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea></td>
</tr>
<tr>
<td>Form Name</td>
<td><input name="txtFormName" type="text"></td>
</tr>
<tr>
<tr>
<td>Form Email</td>
<td><input name="txtFormEmail" type="text"></td>
</tr>
<tr>
<td>Attachment</td>
<td><input name="fileAttach" type="file"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>

What is wrong? is it the php file or the html form?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.