954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please Read! php problem.

I'm just new in php actually and this is the problem that i have been encountering lately. I actually have one .html file and .php file, on the .html file I created a form which looks like this:

<table cellpadding="0" cellspacing="5" border="0" width="500" align="center">
	    <form name="FAQ" method="post" action="question.php">
	    <span style="font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-style: normal;line-height: normal;font-weight: normal;font-variant: normal;text-transform: none;color: #000000;"> 
	    <tr>
			<td width="150"><p class="content">Name: * </td>
			<td width="350"><input type="text" size="55" name="name"></td>
	    </tr>
	    <tr>
			<td width="150"><p class="content">Email Address: * </td>
			<td width="350"><input type="text" size="55" name="email"></td>
	    </tr>
	    <tr>
			<td width="150"><p class="content">Question: * </td>
	    </tr>
	    <tr>
			<td width="350" colspan="2"><textarea cols="59" rows="5"></textarea name="question"></td>
	    </tr>
	    <tr>
			<td colspan="2" align="right"><input type="submit" value="Submit" name="submit"></td>
	    </tr>
	    <tr>
			<td>&nbsp;</td>
	    </tr>
	    </form>
	    </table>


And here is the .php file:

<?php
	if (isset($_POST["submit"]))
	{
		$name = $_POST['name'];
		$email = $_POST['email'];
		$question = $_POST['question'];
		$message = "Message from $name \n Message: $question";
		
		mail($email,Inquiry,$message);	
		echo "Message sent.";		
	}
	else
	{	
		echo "Sending failed, please input the required fields.";
	}
?>


The problem that i'm encountering here is, in each textbox in the.html file i just live it blank, once that i click on the "Submit" button, the output that would be displayed is "Message sent.", instead of "Sending failed, please input the required fields."

Please help me here.. Thanks, been trying to solve it for almost 6hours already, and still i get the same output.

thanks in advance.

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

you should have check first if the fields are not empty?

<?php
// you should check if this fields are empty
if($_POST['name']=="" || $_POST['email']=="" || $_POST['question']=="")
{
echo "Sending failed, please input the required fields.";
}else{
$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['question'];
$message = "Message from $name \n Message: $question";
if(mail($email,Inquiry,$message))
echo "Message sent."; 
else
echo "Message sending failed";
}


For now, you might already get the idea. Hope it helps

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 

I used the code that you suggested but nothing has been displayed or no output at all. What is the use of the isset() function in the if-else statement then?

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
I used the code that you suggested but nothing has been displayed or no output at all. What is the use of the isset() function in the if-else statement then?

you are checking if the post method has been set or is the submit button been submitted.

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 

Ahm, what is then the problem on the code why does it not display anything?

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

the problem is, this part

<td width="350" colspan="2"><textarea cols="59" rows="5"></textarea name="question"></td>


try changing it to this

<td width="350" colspan="2"><textarea cols="59" rows="5" name="question"></textarea></td>


and use the code that i've given earlier

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 

Seriously, still no output. I even tried adding this statement in the .php file

echo "Me";


it actually showed nothing. I added that statement after the <?php tag and before the if-else statement, here:

<?php
	echo "Me";
	if($_POST['name']=="" || $_POST['email']=="" || $_POST['question']=="")
	{
		echo "Sending failed, please input the required fields.";
	}
	else
	{
		$name = $_POST['name'];
		$email = $_POST['email'];
		$question = $_POST['question'];
		$message = "Message from $name \n Message: $question";

		if(mail($email,Inquiry,$message))
			echo "Message sent.";
		else
			echo "Message sending failed";
	}
?>


so confusing..

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Seriously, still no output. I even tried adding this statement in the .php file

echo "Me";

it actually showed nothing. I added that statement after the <?php tag and before the if-else statement, here:

<?php
	echo "Me";
	if($_POST['name']=="" || $_POST['email']=="" || $_POST['question']=="")
	{
		echo "Sending failed, please input the required fields.";
	}
	else
	{
		$name = $_POST['name'];
		$email = $_POST['email'];
		$question = $_POST['question'];
		$message = "Message from $name \n Message: $question";

		if(mail($email,Inquiry,$message))
			echo "Message sent.";
		else
			echo "Message sending failed";
	}
?>

so confusing..


do you have a mailserver? i tested your code on my machine.. and im sure that it is working.

try removing the mail function then try outputting just plain text

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 
if(mail($email,Inquiry,$message))


try changing the 'inquiry'

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 

this is what i did in the .php file, i omitted the contents and i just replaced it with this statement but still no output:

<?php
   echo "Me.";
?>


what do u think is causing the problem?

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

php forms can be self processing, its easy to debug one script, try

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
<title>HTML Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<?php if (isset($_POST["submit"])) 	{
  if(!$_POST['name'] || !$_POST['email'] || !$_POST['question'] )     
  { echo "Required fields not completed, please input the required fields."; }
 else { $name = $_POST['name'];
  $email = $_POST['email'];
  $question = $_POST['question'];
  $message = wordwrap("Message from $name \n Message: $question", 70);
  if(!mail($email,"Inquiry",$message)) {echo "Sending failed, please input the required fields."; } else echo "Message sent.";
 } } ?>
<form name='FAQ' method='post' action='<?php echo $_SERVER["PHP_SELF"]; ?>'>
<table cellpadding='0' cellspacing='5' border='0' width='500' align='center'>
<tr>
<td width='150'><p class='content'>Name: * </td>
<td width='350'><input type='text' size='55' name='name'></td>
</tr>
<tr>
<td width='150'><p class='content'>Email Address: * </td>
<td width='350'><input type='text' size='55' name='email'></td>
</tr>
<tr>
<td width='150'><p class='content'>Question: * </td>
</tr>
<tr>
<td width='350' colspan='2'><textarea cols='59' rows='5' name='question'></textarea></td>
</tr>
<tr>
<td colspan='2' align='right'><input type='submit' value='Submit' name='submit'></td>
</tr>
</table>
</form>
</body>
</html>


but the message is only being sent to the sender, no address is specified for you
Try replacing line 16

if(!mail('myaddress@mysite.com',"Inquiry",$message,"Cc:$email\r\n")) {echo "Sending failed, please input the required fields."; } else echo "Message sent.";


message to "customer service", copy to sender
all necessary html headers are sent before any script output, in case malformed pages are being thrown out by the server,

almostbob
Posting Sensei
3,147 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

this is what i did in the .php file, i omitted the contents and i just replaced it with this statement but still no output:

<?php
   echo "Me.";
?>

what do u think is causing the problem?

can you post the whole code?

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 

this is a better line16

if(!mail('me@mysite.com',"Inquiry","$message","From: $email " . "\r\n" . "Cc: $email"."\r\n")) {echo "Sending failed,"; } else { echo "Message sent."; }
almostbob
Posting Sensei
3,147 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

try this:

<?php
//The button is presed
	if (isset($_POST["submit"]))
	{
		$name = $_POST['name'];
		$email = $_POST['email'];
		$question = $_POST['question'];
		$message = "Message from $name \n Message: $question";
		
		$sent_mail = mail($email,$message,$message);
		if($sent_mail) {
		echo "Message sent.";
		die();//Stop here
		} else {
		"Message was NOT Sent.";
		die();
		}
	}
	else {	
	?>
		<table cellpadding="0" cellspacing="5" border="0" width="500" align="center">
	    <form name="FAQ" method="post" action="mail.php">
	    
	    <tr>
			<td width="150"><p class="content">Name: * </td>
			<td width="350"><input type="text" size="55" name="name"></td>
	    </tr>
	    <tr>
			<td width="150"><p class="content">Email Address: * </td>
			<td width="350"><input type="text" size="55" name="email"></td>
	    </tr>
	    <tr>
			<td width="150"><p class="content">Question: * </td>
	    </tr>
	    <tr>
			<td width="350" colspan="2"><textarea name="question" cols="59" rows="5"></textarea></td>
	    </tr>
	    <tr>
			<td colspan="2" align="right"><input type="submit" value="Submit" name="submit"></td>
	    </tr>
	    <tr>
			<td>&nbsp;</td>
	    </tr>
	    </form>
	    </table>
		
		<?php
		}
		?>


This worked on my server. Tested!

smartness
Junior Poster
103 posts since Aug 2007
Reputation Points: 7
Solved Threads: 14
 

Hi there almostbob, thanks for the quick response.. well actually i'm still having a problem with one line that you've give which is in line 18 stating:

<form name='FAQ' method='post' action='<?php echo $_SERVER["PHP_SELF"]; ?>'>


The problem is in the action='<?php echo $_SERVER["PHP_SELF"]; ?>'
Once that i click the Submit button a "Page Load Error" occurs saying

"Firefox can't find the file at /C:/Documents and Settings/rEd_xiii21/Desktop/programming/ERDI Website Final/Duplicate of Edited Site/<?php echo $_SERVER["PHP_SELF"]; ?>."

What does the code - action='<?php echo $_SERVER["PHP_SELF"]; ?>' - actually do?

Thanks for your time.

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Here is a link to download the .html and .php file that i'm working with.

http://www.adrive.com/public/c5104686df888c76ed104887a1e7be92c68f7c538fba57f8f36addd5e82f624c.html

Ahm, i'm actuallly using wamp server, I've set them ONLINE and started all services too.. so I think I don't have a problem with it though.

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Here is a link to download the .html and .php file that i'm working with.

http://www.adrive.com/public/c5104686df888c76ed104887a1e7be92c68f7c538fba57f8f36addd5e82f624c.html

Ahm, i'm actuallly using wamp server, I've set them ONLINE and started all services too.. so I think I don't have a problem with it though.

I can assure that the code that this guys have given are all working. Your code even works, it prints "ME" after i click the submit button.

Have you put your files in the WWW directory?? in wamp by default, it is located here C:\wamp\www\

then try accessing it like this http://localhost/E-FAQ.html

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 

you are trying to run php from the local file system /C:/Documents and Settings/rEd_xiii21/Desktop/programming/ERDI Website Final/Duplicate of Edited Site/
that wont work, even if it is the same folder
php has to run as http://127.0.0.1/filepath/filename.php (or localhost/)
move all the files you wish to work on, below the folder assigned as localhost in your php/wamp/IDE settings and access them though the localhost url
I do not know what php system version/wamp/ide you have installed but the errors now seem to be simply method problems, but a solution is getting closer (nervous fingers crossed)

almostbob
Posting Sensei
3,147 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

if it still does work, try starting your wamp server :)

ivatanako
Junior Poster
150 posts since Jul 2007
Reputation Points: 18
Solved Threads: 16
 

Whew!! I guess that solves the problem. Well thanks guys, you we're of great help add up more reputations to you guys.. :D

JerieLsky
Junior Poster in Training
63 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You