Jump to content

Recommended Posts

Hi Guys,

I am a PHP virgin and have tried to make a contact form that will update my mysql DB and also email the form too, with a bit of help from Dreamweaver and Adobe forums.

 

I have my contact form but clearly my code is wrong, It renders to the error message asking to go back fill out the details straightaway with out even showing the form?

 

Really Stuck

 

i have removed the email address for privacy in the mail headers

<?php require_once('Connections/webform.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO contactForm (Name, Company, Phone, Email, Notes) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['company'], "text"),
                       GetSQLValueString($_POST['phone'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['notes'], "text"));

  mysql_select_db($database_webform, $webform);
  $Result1 = mysql_query($insertSQL, $webform) or die(mysql_error());
}

mysql_select_db($database_webform, $webform);
$query_update1 = "SELECT * FROM contactForm";
$update1 = mysql_query($query_update1, $webform) or die(mysql_error());
$row_update1 = mysql_fetch_assoc($update1);
$totalRows_update1 = mysql_num_rows($update1);

$errmsg = "";
if (!isset($_POST['name']) || empty($_POST['name'])) $errmsg .= "<p>Please enter your  name"; 
if (!isset($_POST['email']) || empty($_POST['email'])) $errmsg .= "<p>Please enter your email address"; 
if (!isset($_POST['mobile']) || empty($_POST['mobile'])) $errmsg .= "<p>Please enter your mobile phone number";
if ($errmsg!= "") { 
echo $errmsg; 
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>"; 
exit; 
}else { 
echo "<p>success: all fields were filled out"; 
} 
$to = "myemail";
$subject = "Motor Sport-Web Enquiry";
$headers .= "From:email address\r\n"; 
$headers .= "Cc: ccemailaddress\r\n"; 
$headers .= "Return-Path:return email\r\n"; 
$headers .= "Reply-To:return email\r\n";
$message = "Your have received a enquiry from the motorsport \n\n"; 
$message .= "Name: " . $_POST['name'] . "\n"; 
$message .= "Email: " . $_POST['email'] . "\n"; 
$message .= "Company: " . $_POST['company'] . "\n"; 
$message .= "Telephone: " . $_POST['phone'] . "\n\n"; 
$message .= "Mobile: " . $_POST['mobile'] . "\n\n"; 
$message .= "Notes: " . $_POST['notes'] . "\n\n"; 
$message .= "A copy of this email has been sent to  and the details have been saved in your database. \n\n";
if (mail($to,$subject,$message,$headers)) { 
echo "<p>thank you for your time a memeber of the team will contact you shortly!"; 
} else { 
echo "<p>email could not be sent please call us on 0"; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/188043-php-form-to-email-and-update-db/
Share on other sites

Can you post the form as well..

and is "Connections/webform.php" for the database  connection ?

 

the error is due to the following code:~

$errmsg = "";
if (!isset($_POST['name']) || empty($_POST['name'])) $errmsg .= "<p>Please enter your  name";
if (!isset($_POST['email']) || empty($_POST['email'])) $errmsg .= "<p>Please enter your email address";
if (!isset($_POST['mobile']) || empty($_POST['mobile'])) $errmsg .= "<p>Please enter your mobile phone number";
if ($errmsg!= "") {
echo $errmsg;
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
exit;
}else {
echo "<p>success: all fields were filled out";
}

you need to check the form uses POST and contains the following fields name, email & mobile

 

Thank you very much for your reply... yes webform.php the is the connection for the database. I think i spotted an error ? I should not have the mailto and the php called as an action, as the mailto is being dealt with inside the php script?

 

<div id="form">
  <form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?> mailto:emailaddress">
  <fieldset>
  <legend>Contact Details</legend>
  <ol>
  <li>
  <label for="name">Name: <em>required</em></label>
  <input id="Name" name="name" class="text"  type="text"/>
  </li>
  <li>
  <label for="email">Email Address: <em>required</em></label>
  <input id="Email" name="email" class="text" type="text" />
  </li>
  <li>
  <label for="company">Company:</label>
  <input id="Company" name="company" class="text" type="text"  />
  </li>
  <li>
  <label for="phone">Telephone:</label>
  <input id="Number" name="phone" class="text" type="text"  />
  </li>
   <li>
  <label for="mobile">Mobile: <em>required</em></label>
  <input id="Mobile" name="mobile" class="text" type="text"  />
  </li>
  <li>
  <label for="notes">Notes:</label>
  <textarea id="Notes" cols="" rows="6" name="notes" class="text"  > </textarea>
  </li>
  </ol>
  </fieldset>
  <fieldset class="submit">
  <input class="submit" type="submit"  value="Send"/>
  </fieldset>
  <input type="hidden" name="MM_insert" value="form1" />
  </form>
</div>

$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

The exact message that shows are the error messages below. At no point is the form visible.

$errmsg = "";
if (!isset($_POST['name']) || empty($_POST['name'])) $errmsg .= "<p>Please enter your  name"; 
if (!isset($_POST['email']) || empty($_POST['email'])) $errmsg .= "<p>Please enter your email address"; 
if (!isset($_POST['mobile']) || empty($_POST['mobile'])) $errmsg .= "<p>Please enter your mobile phone number";
if ($errmsg!= "") { 
echo $errmsg; 
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>"; 
exit; 
}else { 
echo "<p>success: all fields were filled out";

You need to post from the form to this script..

if your not seeing the form then you have loaded the wrong script or you have either not giving the full picture or

$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

is wrong..

 

The form action should be set to the URI of the script that will process the forms data,

So lets say you have a form.html file that contains the form and a form.php that contains the php script for that form,

you would need to set the action of (in form.html) to form.php. this will send all the post requests from form.html to form.php.

the php is embedded in the document and the form action is

 

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">

 

 

does the following  look like it is written to do the right job ?

 

$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

 

 

 

 

 

the php is embedded in the document

The PHP code will have to be at the bottom, for it to work

 

and the form action is

 

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">

 

 

does the following  look like it is written to do the right job ?

 

$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

I have no idea as that string is concatenated and without seeing the start of it or the results i can't say

Hello,

i started again and just aimed at getting the form to email code to work using an external PHP file. The form to email code works thankfully  :D

i really appreciate your help

 

Now to add to the function so i can update the database too, can i use dreamweaver to generate the php for me and add it to the form to email function?

 

cheers 

 

 

Can i use dreamweaver to generate the php for me and add it to the form to email function?

You could but personally i dislike DM when it comes to writing PHP code,

 

I assume your database has a primary field (I'll assume its ID) and i have taking parts of your code, to create a sample script to allow editing,

assuming the script was called edit.php and you had a field ID of 10, you could edit it via

edit.php?ID=10

 

Heres the sample

<?php require_once('Connections/webform.php'); ?>
<?php
$ID = $_GET['ID'];
mysql_select_db($database_webform, $webform);

//Save Data
if(isset($_POST['submit'])){
$query_update = sprintf("UPDATE contactForm SET (`name`,`email`) VALUES ('%s','%s') WHERE ID = %d",
mysql_real_escape_string($_POST['name']),
mysql_real_escape_string($_POST['email']),
$ID);
mysql_query($query_update, $webform) or die(mysql_error());
}
//Get Data
$query_select = sprintf("SELECT * FROM contactForm WHERE ID = %d",$ID);
$select = mysql_query($query_select, $webform) or die(mysql_error());
$row_select = mysql_fetch_assoc($select);
?>
<div id="form">
  <form id="form1" name="form1" method="POST" action="?ID=<?php echo $ID; ?>">
  <fieldset>
  <legend>Contact Details</legend>
  <ol>
  <li>
  <label for="name">Name: <em>required</em></label>
  <input id="Name" name="name" class="text"  type="text" value="<?php echo $row_select['name'];?>"/>
  </li>
  <li>
  <label for="email">Email Address: <em>required</em></label>
  <input id="Email" name="email" class="text" type="text" value="<?php echo $row_select['email'];?>"/>
  </li>
  <fieldset class="submit">
  <input class="submit" type="submit"  value="Update"/>
  </fieldset>
  </form>
</div>

 

Please not i have had to take a few guesses and this is untested but should help you on your way.

 

[ot]Oh and the reason I dislike DM is because it seams to give to less control and is very generic, a guy I work with only knew PHP via DM and after a I gave him little training he hates the DM generated code and now he only use DM for page design,

if you understand all the code it creates then fine but most do not and those who do rarely allow DM to create their code, but these are my own opinions. [/ot]

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.