i am having some problem with my PHP form . I have created everything and its posting the details to my mailbox the problem however is that in the message area all it shows is $text , it doesn't show what the visitor typed into that area all it shows is $text

here is the form

<html>
<head><title>contact us </title>
</head>
<body><form action="step2.php" method ="post">
Name<br/><input type="text" name="username" /><br/>
Email<br/><input type="text" name="email" /><br/>
Inquiry<br/><textarea row="7" cols="70" name="inquiry" /></textarea><br/>
<input type="submit"value="send your inquiry" /></form>
</body>
</html>

here is the PHP

<?php

$name = $_POST['username'];
$email = $_POST['email'];
$text = $_POST['inquiry'];

//To, Subject, Message, Header
mai('myemail@mywebsite.com','Inquiry',$text,'From: '. 

$name .'<' . $email . '>');
'>');

header('location: step3.html')

?>

is there anything i am doing wrong ? Please if there is a better way which i can make a better submit form / PHP script please assist me . i would appreciate your help.

Put the variable $text in quotes as well, "$text".

I don't know why you would be getting the word $text unless you had it in single quotes in your code. Someone told me that PHP will eval variables in double quotes but not variables in single quotes. I've never really paid attention.

edit: The code you posted should work, I use something very similar on my own website and it works fine. I wonder if maybe you changed the code when you posted it here?

In general I try to always pass parameters to PHP functions in double quotes, although inside the double quotes you can use single quotes and it will expand them just fine.


mysql_query("insert into dbusers (name,password) values ('$uname','$upass')");

Since the entire thing is inside double quotes it expands the variables even though they are in single quotes.

hi dietdew . thumbs up , you rock !!!!! i added the double quotes and it worked . I spent 72hrs bothering over a simple php script that was just missing double quotes (although i am a noob so you can pardon me lol) . Anyway to more questions
1.i would like to make it compulsory for values to be filled into a textbox for example if the name / email is empty the form doesn't post and an error message tells them to fill up the space .
2.Also i would like to make the name space to accept on alpahbetic values

Thank you. :) That happens to all of us from time to time.

Validating form fields can be done with PHP alone, but it's better to use javascript. It's almost midnight here, I'll see what I can dig up tomorrow AM.

You might search 'js validate form' if you want a head start on me.

:) I've edited this post because I found the problem. It should work properly now.

In the <form> statement you include the 'onSubmit="return validate()"' and that calls the js function validate which returns either a true or a false. If it's true then the form submits. I had to borrow the alphanumeric script from hscripts.com because I'm lazy. :)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>contact us </title>
<script type="text/javascript">
// Fuction alphanumeric by hscripts.com
function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else
			{
			return false;
		  }
 		}
 return true;
}

function validate()
 {
 var uname = document.forms.contact.username.value;
 var email = document.forms.contact.email.value;
 var inquire = document.getElementById('inquire').innerHTML;
 var message="";
 if (!alphanumeric(uname))
  {
	message="Name field must contain letters and numbers only.<br />";
	} 
 if (uname=="")
  {
	message="Value missing for 'Name' field.<br />";
	}
 if (email=="")
  {
	message=message+"Value missing for 'Email' field.<br />";
	}
 if (inquire="")
  {
	message=message+"Value missing for the 'Inquiry' field.<br />";
	}
 if (message!="")
  {
  document.getElementById('message').innerHTML=message;
	return false;
	}
 else
  {
  return true;
	}
 }
 
</script>
</head>
<body>
<span id="message"></span><br />
<form name="contact" action="step2.php" method ="post" onSubmit="return validate()">
Name<br/>
<input type="text" name="username" /><br/>
Email<br/>
<input type="text" name="email" /><br/>
Inquiry<br/>
<textarea id='inquire' row="7" cols="70" name="inquiry" /></textarea><br/>
<input type="submit" value="send your inquiry" />
</form>
</body>

Hi Dietdew , i added new fields such as country (drop down list ), phone (edit box), address (editbox), state(editbox) , how did you find us (drop downlist), how do i get my PHP scripts to recognize this fields because all it recognizes and posts to my email are name , email and inquiry

Now, I can't do everything for you. ;)

You do the edit boxes the same way you did the name, email, and inquiry in the step2.php file. Use the $_POST from the previous page.

Drop downs are little more involved, but they are easy enough that I'm going to let you figure that out on your own. I will point you to the w3schools page for select tags, though. http://www.w3schools.com/tags/tag_select.asp

As for the javascript, I made it very basic with the 'var uname = document.forms.contact.username.value;' and the if{}. You can just add more variables and if{}s or try to find a better way(there is a much better way for numerous form elements)

Happy programming, if you run into trouble you can post again.

David

Hi so does the select tag come into my HTML or PHP script . If its my PHP script can you please explain how

It goes in as a part of the form on the html page.

w3schools missed something on their page, though. Your select tag needs a name to pass to the PHP page.

<select name='itemname'>
<option value='value1 />
etc. . .

Then the $_POST contains the value of the option they select.

Hi dietdew , i was able to do the editbox stuff and its working well for me now , however the dropdown list i haven't been successful with that , i am confused as to how get my script to recognize this fields .

Show me what you've got so far, I remember that validating a select tag is a lot different than an editbox.

I was bored and started playing with this code again, don't know if it'll help but thought I'd post it just in case.

step1.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>contact us </title>
<script type="text/javascript">
// Fuction alphanumeric by hscripts.com
function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else
			{
			return false;
		  }
 		}
 return true;
}

function validate()
 {
 var uname=document.contact.Username.value;
 var message="";
 if (!alphanumeric(uname))
  {
	message="Name field must contain letters and numbers only.<br />";
	} 

 for(i=0; i<document.contact.elements.length; i++)
  {
	if (!document.contact.elements[i].value)
	 {
	 message+="Missing value for "+document.contact.elements[i].name+"<br />";
	 }	  
	}
		
 if (message)
  {
  document.getElementById('message').innerHTML=message;
	return false;
	}
 else
  {
  return true;
	}
 }
 
</script>
</head>
<body>
<span id="message"></span><br />
<form name="contact" action="step2.php" method ="post" onSubmit="return validate()">
Name<br/>
<input type="text" name="Username" /><br/>
Email<br/>
<input type="text" name="Email" /><br/>
Inquiry<br/>
Country<br />
<select name='Country'>
<option value='' style='font-style: italic;'>Select. . .</option>
<option value='Here'>Here</option>
<option value='There'>There</option>
<option value='Everywhere'>Everywhere</option>
</select><br />
<textarea row="7" cols="70" name="Inquiry" /></textarea><br/>
<input type="submit" value="send your inquiry" />
</form>
</body>

and the 2nd page,

step2.php

<?php

$name = $_POST['Username'];
$email = $_POST['Email'];
$text = $_POST['Inquiry'];
$place= $_POST['Country'];

//To, Subject, Message, Header
echo "<br />Name: $name; Email: $email; Message: $text Location: $place<br />";

?>

Instead of using an if{} for every value, you can step through them by elements[#] so no matter how many fields you put in your form it will still check them. Pay attention to the 'select' tag. The first option has a value of "" or nothing. I put that there to make sure the user selects something, the javascript wouldn't ever say anything if that value was anything but "".

Anyway, hope that helps.

Hi dietdew here is the PHP code .

<?php

$name =$_POST['username'];
$email =$_POST['email'];
$phone=$_POST['phone'];
$subject=$_POST['Subject'];
$addressline1=$_POST['address1'];
$addressline2=$_POST['address2'];
$state=$_POST['state'];
$city=$_POST['city'];
$country=$_POST['country'];


$message= "Form Message \n\n
Name: $name\n
Email: $email\n
Phone: $phone\n
Address line1: $addressline1\n
Address line2: $addressline2\n
State: $state\n
city: $city\n
Country:  $country\n";





//To, Subject, Message, Header
mail('email@mywebsite.com',"$subject" ,"$message",'From: '. 

$name .'<' . $email  . '>');

header('location: step3.html')

?>

the fields available at the HTML code are name , email , phone , address line1 , address line 2 , city , state , country . How do i add a validation code for this fields

Hi,

I have analysed your code of both html and php file. Thre is some problem in both of files i found. the working code is below

htmlfile
--------------

<html>
<head><title>contact us </title>
</head>
<body>
<form action="step2.php" method ="post">
Name<br/><input type="text" name="username" /><br/>
Email<br/><input type="text" name="email" /><br/>
Inquiry<br/>
<textarea row="7" cols="70" name="inquiry" ></textarea>
<br/>
<input type="submit"value="send your inquiry" />
</form>
</body>
</html>

phpfile
--------

<?php
   
       
   
      $name = $_POST['username'];
   
      $email = $_POST['email'];
   
      $text = $_POST['inquiry'];
	  print $name;
	  print $email;
	  print $text;
       
      ?>

^^^^^thanks . do you know about form validation

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.