Forum Moderators: coopster

Message Too Old, No Replies

Php Contact Using Mail Function

Please Help Me

         

dc_duo

2:37 pm on Aug 30, 2004 (gmt 0)

10+ Year Member



Just started to learn PHP got quite far can now use PHP with MYSQL quite well however at the moment on my site I have created I have a classifieds section where all the information regarding different items is stored and can be called up.

One of the selections is to contatc seller this at the moment is set to a mailto HTML command which is not good considering that it gives everyones email address away at the moment.

I am trying to build a bit of script that will get the id from the classified then open another page which will pull all the information off of the MYSQL databse I can do this but executing the mail () command is slightly more harder for me the script works until you press submit and then loads the page again (form action="post") this is where the code ceases. Below is the code please tell me what I am doing wrong.

<?
// error handling

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

require_once ('../includes/connect.php');

// 20th August 2004 - Changed $get var to $id also changed row email to $mail. Put in header for email sent

// Define the query

$query = "SELECT * FROM diecast_sale WHERE item_no={$_GET['id']}";
if ($r = mysql_query ($query)) { // Run the query

$mail = $row['email'];

$row = mysql_fetch_array ($r);


mysql_close(); // Close the db connection

} else {

print "<p>Could not retrive the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";

}

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

$problem = FALSE; // No problems so far.

// Check for each value.
if (empty ($_POST['username']) ) {
$problem = TRUE;
print '<p><font color="red">Please enter your name or your username!</font></p>';
}

if (empty ($_POST['email'])) {
$problem = TRUE;
print '<p><font color="red">Please enter your email address!</font></p>';
}

if (empty ($_POST['enquiry']) ) {
$problem = TRUE;
print '<p><font color="red">Please enter an enquiry!</font></p>';
}

if (!$problem) {

// send the email

$body = "Question from {$_POST['username']}. enquiry {$_POST['enquiry']}. From Email {$_POST['email']}.";
if (@mail ($mail , 'Enquiry from NDUK user!' , $body )) {

print '<p align="center"><b>Thank you for your enquiry!</b></p>';

}

} else {

print '<p><font color="red">Please try again!</font></p>';

}

} // End of handle form

// Make the form

print' <form action="contactseller.php" method="post">

<table width=100% cellpadding=5 cellspace=5 border=0 bgcolor=#e3e3e3 align="center">
<tr>
<td valign="top">Your Username:</td>
<td valign="top">
<div align="left">
<input type="text" name="username" size="20" />
</div>
</td>
</tr>

<tr>
<td valign="top">Your Email Address:</td>
<td valign="top">
<div align="left">
<input type="text" name="email" size="40" />
</div>
</td>
</tr>

<tr>
<td valign="top">Enquiry:</td>
<td valign="top">
<div align="left">
<textarea name="enquiry" cols="50" rows="10"></textarea>
</div>
</td>
</tr>


<tr>
<td valign="top"></td>
<td valign="top">
<div align="left">
<input type="hidden" name="id" value="' . $GET['id'] . '" />
</div>
</td>
</tr>

<tr>
<td valign="top"></td>
<td valign="top">
<div align="left">
<input type="submit" name="submit" value="Send your question!" />
</div>
</td>
</tr>
</table>

</form>';
?>

Timotheos

8:23 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see anything obvious. Are you getting any error messages? Maybe you could put some echo statements at various places to see where it's choking.

StupidScript

10:21 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only thing I see is when you print the hidden "id" form element (in a table row, for some reason):

You have entered: ( value="'.$GET['id'].'" )

I think you may mean: ( value="'.$_GET["id"]."'" )

That is enough to cause the form to NOT display, even though the mail is sent successfully, several lines earlier in the script.

jatar_k

10:29 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld dc_duo,

this bit looks suspect

$query = "SELECT * FROM diecast_sale WHERE item_no={$_GET['id']}";
if ($r = mysql_query ($query)) { // Run the query
$mail = $row['email'];

$row = mysql_fetch_array ($r);

the lines are a little mixed up there, try it this way

$query = "SELECT * FROM diecast_sale WHERE item_no={$_GET['id']}"; 
$r = mysql_query ($query) or die (mysql_error());
$numrows = mysql_num_rows($r);
if ($numrows < 1) {
$problem = TRUE;
print '<p><font color="red">Could not find the email address</font></p>';
} else if ($numrows > 1) {
$problem = TRUE;
print '<p><font color="red">More than one record found</font></p>';
} else {
mysql_data_seek($r,0);
$row = mysql_fetch_array($r);
$mail = $row['email'];
}

I added in the numrows to check that only one row is returned just in case. Maybe that will help give us a better idea where the problem is.

dc_duo

5:31 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



The problem I am getting with the script is that the mail is not being sent. When you click submit it posts the form and then reloads as usual what I think is happening is that the id from the MYSQL part is not being carried over when the user clicks the submit button.

When you click the submit button the browser reloads the page because of the form action part (Is this required?) but when the page reloads I get an error saying:-

There is a problem with your MYSQL syntax this is because the id has not carried over so there is an immedate error

p1lky

5:49 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



When the form is submitted it sends a $_POST[id], not a $_GET[id]. You're using $_GET[id] in the sql but once the form is submitted it isn't there.

dc_duo

6:08 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



Any ideas? This is really start to miff me off. What essentially I am trying to do is to do like ebays contact seller system.

I grab the id from the classified that is tehn picked up by this code. Have I got in the wrong order should the

if (isset submit part go first?

I am really confused am I making harder work for myself.

I appreciate all the help you are giving me

p1lky

6:21 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



change to
// Define the query

if ($_GET['id'])
{
$query = "SELECT * FROM diecast_sale WHERE item_no={$_GET['id']}";
}
elseif ($_POST['id'])
{
$query = "SELECT * FROM diecast_sale WHERE item_no={$_POST['id']}";
}

jatar_k

7:27 pm on Aug 31, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the fix I posted was because you are setting this

$mail = $row['email'];

but you are doing it in the wrong place and there would be no value assigned to that var given the code that you posted.

You need to diagnose the reason for the mail not getting sent. Start with the values that you are passing to the mail function to see if you are assigning values properly. If they are all correct then you look at mail malfunctions after that.