Jump to content

Additional help with form


olly79

Recommended Posts

Hi all,

 

I just wonder if someone could help me with the following:

 

I now have the following as my webform:

 

<form id="form1" method="post" action="contactprocess.php" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="text" name="subject" />
<textarea cols="2" rows="2" name="message"></textarea>
<a href="javascript:void(0);" class="link-1 form-link" onclick="document.getElementById('form1').reset()">Reset</a><a href="javascript:void(0);" class="link-1" onclick="document.getElementById('form1').submit()">Submit</a>  

 

And this is my PHP code:

 

<?php
  //stole the email function
  function checkemail($eemail){
    if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$eemail)) return true;
    else return false;
  }

  //post script
  if($_SERVER['REQUEST_METHOD'] == "POST"){
    $errors = array();
    foreach($_POST as $key => $value){
      //check if fields are empty
      if($value == ""){ $errors[] = $key . " was blank"; }else{
        if(strpos($key, "email") !== false && !checkemail($value)){
          $errors[] = $value . " was not a correct email";
        }
      }
    }
    //see if we have errors and display them
    if(count($errors)){
      //display errors.
?>
<h2>we encountered the following errors while submitting your form:</h2>
<ul>
<?php foreach($errors as $field => $error){ ?>
  <li><?php echo $error; ?></li>
<?php } ?></ul><?php
    }else{
      //submit the form
      //(this code is from the original script. if you would be emailing it, you would just need to change all of the variable names.
      //change the $_POST['_whatever_'] part to whatever you named the fields on your form. and get rid of the mutli-line comments (/* and */)
      /*
      $mymail = "[email protected]";
      $ename = $_POST['ename'];
      $eemail = $_POST['eemail'];
      $esubject = $_POST['esubject'];
      $emessage = $_POST['emessage'];

      $body = "$emessage\n\nName: $ename\nEmail: $eemail\nIp: $eip";
      mail($mymail,$esubject,$body,"From: $eemail\n");
      */
      echo "thank you for submitting the form.";
    }
  }else die("Error!");
?> 

 

I have a few areas I would like help with.

 

1) Errors - Instead of displaying errors as per the following: <h2>we encountered the following errors while submitting your form:</h2> I would like these to be in a pop up window as opposed to what curently happens. I would also like to use the same method for the following: echo "thank you for submitting the form.";. At present you are currently direct away from the site to a page which simply displays the message.

 

2) MySQL - As well as sending the data to email I would also like to send it to a database for use later on; therefore can anyone help me on how to include this. I have started to look at writing some SQL for this below; therefore again I would be grateful if someone could help me with this (p.s. this is not correct; however, it was simply me attempting to write what I think it should be, but I need to change the last string to the correc fields):

 

 

$link = mysql_connect("localhost", "root","password") or die ("Unable to connect to database.");
mysql_select_db("contacts") or die ("Unable to select database.");
$sql="SELECT id FROM contacts WHERE username = '$usrnm' or email = '$email'";
$result = mysql_query($sql) or die ("Couldn't get results.");
$num = mysql_numrows($result);
if($num==1 || $num==2) {
    print "&res=Username or email already in user $num";
}elseif($num==0){
    print "&res=User added to database $num";
    $sqlstatement= "INSERT INTO CONTACTS (username, email, password, firstname, lname, gender, country) VALUES ('$usrnm', '$email', '$pw', '$fname', '$lastname', '$gender', '$country')";
     $newquery = mysql_query($sqlstatement, $link);

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/143401-additional-help-with-form/
Share on other sites

to add the popup alert you'll need to include javascript, which i've got no clue about, to your code, also the php validation has to be in the same file as the mainpage

<?php
    // Validation goes here
    if (OK) { javacode }
    elseif (FAILED) { javacode }
?>
-----
main form stuff goes here

 

as for the MYSQL you could do this

 

function DbConnect() {
mysql_connect('host','user','pass');
mysql_select_db('dbname');
}

function IsUser($username) {
$sql = "SELECT `username` FROM `contacts` WHERE `username`='".$username."'";
$result = mysql_query($sql);
$match = mysql_num_rows($result);
return $match;
}

$userExists = IsUser($username);
if ($userExists > 0) {
    // Tell user to find another username
} else {
    // Do all the rest of the registration here
}

 

this is really quick written, no testing or anything, but it'll guide you in the right direction i hope :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.