Form Processing..

CannonBallGuyCannonBallGuy ModeratorShared Hoster
Okay...
What I want seems really complicated to me but it's probablky not so hard...

Okay. I have a HTML Form...
Someone types in their email, name, etc and chooses something from a dropdown menu or radio buttons...
That ^ I can do...

Then, a PHP Script sends an email to the email address provided showing all the other details the person put in with a link to confirm it.

When they click that link two things happen:
An email is sent to me with all the persons info,
And All their info is stored in a MySQL Database.

How the hell do I do all that?
«1

Comments

  • PythonPython Forum Leader The Royal RAM
    hmm, i could probably figure it out how to do it but it would take me ages.

    perhaps if i get some spare time ill give it a go

    what fields do you want the database to have, otherwords what data are you going to collect altogether?

    The Royal Ram

  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Let me try... I've not actually had the chance to run the code through a PHP engine so there may be 1 or 2 bugs (I hope not though).


    1. Create a html form using some edit fields and a Post button.

    2. Name all the edit fields - e.g. the Email field is named "email".

    3. For the form, <form name=[YOUR_FORM_NAME] action=[YOUR_PHP_FILE - e.g. return.php] method=post>

    4. When the user clicks on the button, [YOUR_PHP_FILE - e.g. return.php] will be called

    5. Create a PHP file - e.g. return.php

    6. In the PHP file, we need to do a few things

    6.a. Check to see if all the mandatory fields are filled in

    [Assume your form in your html file has 3 edit fields - name, age, email]

    E.g.

    if($email==""){
    echo "<link href=../styles.css rel=stylesheet type=text/css>
    <center>
    <table width=300 border=0 cellspacing=0 cellpadding=0>
    <tr>
    <td height=30 align=center bgcolor=#FFFFFF>
    <br>Sorry, please enter your email.<br>
    Please <a href=[WHERE YOU WANT TO REDIRECT THE USER TO]>click here</a> to try again.<br><br>
    </td>
    </tr>
    </table>
    </center>";
    exit();
    }

    6.b. If all fields are filled in properly, continue processing...

    [You need to supply $db_host,$db_usr,$db_pwd,$db_name]
    [Assume your table has only 3 fields - field1, field2, field3]


    $id_link=mysql_connect($db_host,$db_usr,$db_pwd);
    mysql_select_db($db_name);


    $q="INSERT INTO [YOUR_TABLE_NAME] (field1,field2,field3) VALUES('$name','$age','$email')";
    $r=mysql_query($q);
    if(!$r) {
    $err=mysql_error();
    echo $err;
    exit();
    }
    else {
    &msg = "[THE CONTENT OF YOUR EMAIL]"
    &sender = "[YOUR EMAIL'S RECIPIENT WILL SEE THIS AS THE SENDER]"
    mail("$email","[YOUR_EMAIL_TITLE]","$msg","From: $sender");
    }
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay... I'm finding some of that a little confusing...

    This is the form I would use:
    [code:1:3d4bc2ba4a]
    <html>
    <head>
    <title>Form...</title>
    </head>
    <body>
    <form name="input" action="process.php" method="post">

    First Name: <input type="text" name="firstname"><br />
    Last Name<input type="text" name="lastname"><br />
    eMail:<input type="text" name="email1"><br />
    eMail again (to confirm):<input type="text" name="email2"><br />
    Payment:<br />
    PayPal:<input type="radio" name="sex" value="PayPal"><br />
    MasterCard:<input type="radio" name="sex" value="MasterCard"><br />

    <input type ="submit" value ="Submit Now!">

    </form>
    </body>
    </html>
    [/code:1:3d4bc2ba4a]
    Can you try and build the code around that?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Hmm..

    1. You've got the form - OK.
    2. When the user clicks on the "Submit Now" button, process.php will get called.

    3. In, process.php, put the code snippets I posted earlier to check that the user has entered values for the mandatory fields

    E.g.
    if($email==""){
    echo "<link href=../styles.css rel=stylesheet type=text/css>
    <center>
    <table width=300 border=0 cellspacing=0 cellpadding=0>
    <tr>
    <td height=30 align=center bgcolor=#FFFFFF>
    <br>Sorry, please enter your email.<br>
    Please <a href=[WHERE YOU WANT TO REDIRECT THE USER TO]>click here</a> to try again.<br><br>
    </td>
    </tr>
    </table>
    </center>";
    exit();
    }

    4. In process.php, put the code snippets for inserting data into the database. If not successful, display an error. If successful, send out an email.

    $id_link=mysql_connect($db_host,$db_usr,$db_pwd);
    mysql_select_db($db_name);

    $q="INSERT INTO [YOUR_TABLE_NAME] (field1,field2,field3) VALUES('$name','$age','$email')";
    $r=mysql_query($q);
    if(!$r) {
    $err=mysql_error();
    echo $err;
    exit();
    }
    else {
    &msg = "[THE CONTENT OF YOUR EMAIL]"
    &sender = "[YOUR EMAIL'S RECIPIENT WILL SEE THIS AS THE SENDER]"
    mail("$email","[YOUR_EMAIL_TITLE]","$msg","From: $sender");
    }



    In summary, you need 2 files - you've already got the html file OK. Create another file with a PHP extension and insert the code I posted.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay... So what exactly would my process.php file look like?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Caveat - I've not tested the code out as I do not have a local PHP engine - however, what I've got below should be fairly good for this simple task. Just cut and paste everything below this line into a new file named process.php. Make sure you fill in the blanks....


    ****************
    cut the code below
    ****************


    <?php
    include_once("config.php"); // if you have a config file

    // one check for each field
    if($email==""){
    echo "<link href=../styles.css rel=stylesheet type=text/css>
    <center>
    <table width=300 border=0 cellspacing=0 cellpadding=0>
    <tr>
    <td height=30 align=center bgcolor=#FFFFFF>
    <br>Sorry, please enter your email.<br>
    Please <a href=[WHERE YOU WANT TO REDIRECT THE USER TO]>click here</a> to try again.<br><br>
    </td>
    </tr>
    </table>
    </center>";
    exit();
    }

    // more checks if you have multiple mandatory fields
    //...
    //...


    $id_link=mysql_connect($db_host,$db_usr,$db_pwd);
    mysql_select_db($db_name);

    $q="INSERT INTO [YOUR_TABLE_NAME] (field1,field2,field3) VALUES('$name','$age','$email')";
    $r=mysql_query($q);
    if(!$r) {
    $err=mysql_error();
    echo $err;
    exit();
    }
    else {
    &msg = "[THE CONTENT OF YOUR EMAIL]"
    &sender = "[YOUR EMAIL'S RECIPIENT WILL SEE THIS AS THE SENDER]"
    mail("$email","[YOUR_EMAIL_TITLE]","$msg","From: $sender");
    }

    ?>


    <html>

    // put html code here - this is what the user will see after the PHP portions have been processed
    // assuming that there were no errors
    // if there were errors the exit(); + echo() functions would have kicked in

    </html>
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    CBG,

    Sorry to double-post.

    To make things clearer - there are 2 files

    File 1 - html that contains the form

    File 2 - php - that contains 2 sections
    Section 1 - PHP - the code enclosed by <?php ?>
    Section 2 - HTML - after the PHP portions have executed, the user will see the html portion.

    :idea:
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Yeah...okay...
    I did everything you said but I'm waiting for my DNS's to change...
  • SEbasicSEbasic Advanced User VPS - Virtual Prince of the Server
    Then type in the IP address of your server.
    Rave it up.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay... I kept for getting to reply to this, but I did as you said and I just get this error:
    Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/matsim68/public_html/process.php on line 13
    

    See for yourself at www.kaziaz.com/form.html
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Please <a href="form.html">click here</a> to try again.<br><br>
    
  • PythonPython Forum Leader The Royal RAM
    If its inside the php tags then it should be like this:
    print "Please <a href=\"form.html\">click here</a> to try again.<br><br>";
    


    That will output it using PHP. notice that theres a slash before the html ". This stops php processng them

    The Royal Ram

  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    I copied exactly what you have above and I still get this:
    Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/matsim68/public_html/process.php on line 13
    
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    The statement is already in an echo command - just replace the original line with the one below - the only difference is the additional slashes. Don't put in the print command.

    Please <a href=\"form.html\">click here</a> to try again.<br><br>
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay. That bit seems to work now, but then I got this:
    Parse error: parse error, unexpected '&' in /home/matsim68/public_html/process.php on line 37
    

    Line 37 is:
    &msg = "Please confirm..."
    
    I tried taking out the & and got this:
    Parse error: parse error, unexpected '=' in /home/matsim68/public_html/process.php on line 37
    
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Put a semi-colon (i.e. ;) behind line 37. Don't remove the & from msg. The & tells PHP that msg is a variable.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Still get the same error :(
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    LOL... I must be blind and blur...

    It's not &.. use $. Replace line 37 with the line below. Take note of the $ and the ;

    $msg="Please confirm...";
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay. Changed that, now I get:
    Warning: main(config.php): failed to open stream: No such file or directory in /home/matsim68/public_html/process.php on line 2
    
    Warning: main(): Failed opening 'config.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/matsim68/public_html/process.php on line 2
    

    Line 2 is:
    include_once("config.php"); // if you have a config file
    
    I don't have a config file so I deleted the line...

    Now it says:
    Sorry, please enter your email.
    Please click here to try again.
    
    even though I am entering my email adress correctly...
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Well, it would seem that now all the syntax bugs are out of the way.

    You say you entered an email address in your form in the html file. Did you name the email field with the same name as the email variable in the PHP file?

    The problem should be trivial. Either you post the relevant sections of the code in this thread or send it to me via email - I'd be glad to have a look at it for you.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay. Here is both files:

    form.html:
    <html>
    <head>
    <title>Form...</title>
    </head>
    <body>
    <form name="input" action="process.php" method="post">
    
    First Name: <input type="text" name="firstname"><br />
    Last Name<input type="text" name="lastname"><br />
    eMail:<input type="text" name="email1"><br />
    eMail again (to confirm):<input type="text" name="email2"><br />
    Payment:<br />
    PayPal:<input type="radio" name="payment" value="PayPal"><br />
    MasterCard:<input type="radio" name="payment" value="MasterCard"><br />
    
    <input type ="submit" value ="Submit Now!">
    
    </form>
    </body>
    </html>
    

    process.php:
    
     <?php 
     include_once("config.php"); // if you have a config file 
    
     // one check for each field 
     if($email==""){ 
     echo "<link href=../styles.css rel=stylesheet type=text/css> 
     <center> 
     <table width=300 border=0 cellspacing=0 cellpadding=0> 
     <tr> 
     <td height=30 align=center bgcolor=#FFFFFF> 
     <br>Sorry, please enter your email.<br> 
     Please <a href=\"form.html\">click here</a> to try again.<br><br>
     </td> 
     </tr> 
     </table> 
     </center>"; 
     exit(); 
     } 
    
     // more checks if you have multiple mandatory fields 
     //... 
     //... 
    
    
     $id_link=mysql_connect($db_host,$db_usr,$db_pwd); 
     mysql_select_db($db_name); 
    
     $q="INSERT INTO xxx (field1,field2,field3) VALUES('$name','$age','$email')"; 
     $r=mysql_query($q); 
     if(!$r) { 
     $err=mysql_error(); 
     echo $err; 
     exit(); 
     } 
     else { 
     &msg = "Please confirm...";
     &sender = "SENDER999";
     mail("$email","Title999","$msg","From: $sender"); 
     } 
    
     ?> 
    
    
     <html> 
    <head><title>Process---</title></head>
    <body>
    <h2>I like Candy!</h2>
    <br /><br />
    
     // put html code here - this is what the user will see after the PHP portions have been processed 
     // assuming that there were no errors 
     // if there were errors the exit(); + echo() functions would have kicked in 
     
    </body>
     </html>
    

    In my form I have email1 and email2
    So I guess I need something like:

    if $email1 == $email2 send email.file to the $email1
    else print "Please check your emails..."

    Obviously I'm new to PHP... lol
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    CBG,

    In your html file you got 2 fields with the names email1 and email2. These fields get converted into variables in your PHP file - $email1 and $email2.

    As such, when you perform mandatory field checking, you need to specify the conditions:

    if $email1==""{
    complain to the user
    exit();
    }

    if $email2==""{
    complain to the user
    exit();
    }

    if $email1==$email2 {
    send email
    }
    else {
    complain to the user
    exit();
    }

    Your mistake is that you used $email. There is no such field in you HTML file and as such PHP created a new variable which is initialized to "".
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay. Thanks.
    So what exactly do I add to the php file, and where exactly do I add it?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    You should be able to copy and paste the code below straight into you PHP file.
    1. I've removed the line for config.php since you mentioned that you do not have one
    2. I've only done checking for emails - you may want to extend the checking for you other input fields in the form - e.g. name, etc.
    3. There is a more efficient way of coding the checking but the one I've set out below should be the easiest to understand.

    ******************
    copy below
    ******************

    <?php

    if($email1==""){
    echo "<link href=../styles.css rel=stylesheet type=text/css>
    <center>
    <table width=300 border=0 cellspacing=0 cellpadding=0>
    <tr>
    <td height=30 align=center bgcolor=#FFFFFF>
    <br>Sorry, please fill in both email fields.<br>
    Please <a href=\"form.html\">click here</a> to try again.<br><br>
    </td>
    </tr>
    </table>
    </center>";
    exit();
    }

    if($email2==""){
    echo "<link href=../styles.css rel=stylesheet type=text/css>
    <center>
    <table width=300 border=0 cellspacing=0 cellpadding=0>
    <tr>
    <td height=30 align=center bgcolor=#FFFFFF>
    <br>Sorry, please fill in both email fields.<br>
    Please <a href=\"form.html\">click here</a> to try again.<br><br>
    </td>
    </tr>
    </table>
    </center>";
    exit();
    }

    if($email1==$email2){
    $id_link=mysql_connect($db_host,$db_usr,$db_pwd);
    mysql_select_db($db_name);

    $q="INSERT INTO xxx (field1,field2,field3) VALUES('$name','$age','$email')";
    $r=mysql_query($q);
    if(!$r) {
    $err=mysql_error();
    echo $err;
    exit();
    }
    else {
    &msg = "Please confirm...";
    &sender = "SENDER999";
    mail("$email","Title999","$msg","From: $sender");
    }
    }
    else {
    echo "<link href=../styles.css rel=stylesheet type=text/css>
    <center>
    <table width=300 border=0 cellspacing=0 cellpadding=0>
    <tr>
    <td height=30 align=center bgcolor=#FFFFFF>
    <br>Sorry, please ensure that both email values are the same.<br>
    Please <a href=\"form.html\">click here</a> to try again.<br><br>
    </td>
    </tr>
    </table>
    </center>";
    exit();
    }

    ?>


    <html>
    <head><title>Process---</title></head>
    <body>
    <h2>I like Candy!</h2>
    <br /><br />

    // put html code here - this is what the user will see after the PHP portions have been processed
    // assuming that there were no errors
    // if there were errors the exit(); + echo() functions would have kicked in

    </body>
    </html>
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Thats great thanks. Everythigns working except the Database bit:
    $id_link=mysql_connect($db_host,$db_usr,$db_pwd); 
     mysql_select_db($db_name); 
    
     $q="INSERT INTO xxx (field1,field2,field3) VALUES('$name','$age','$email')"; 
     $r=mysql_query($q); 
     if(!$r) { 
     $err=mysql_error();
    

    I can guess what most of it should be:
    $id_link=mysql_connect($[b]db_host[/b],$database-username,$database-password); 
     mysql_select_db($database-name); 
    
     $q="INSERT INTO [b]xxx (field1,field2,field3)[/b] VALUES('$name','$age','$email')"; 
     $r=mysql_query($q); 
     if(!$r) { 
     $err=mysql_error();
    

    What do I need to do with the stuff in bold?
  • PythonPython Forum Leader The Royal RAM
    note to bimmercenter:

    when posting code post it using the code tags.

    Example:
    your code here
    

    The Royal Ram

  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    1. Anything preceded by a $ is a variable which you can assign values to
    2. $db_host is the name of your host - e.g. bimmercenter.com

    So you can either:
    $db_host="bimmercenter.com";
    $id_link=mysql_connect($db_host,$database-username,$database-password);
    

    or
    $id_link=mysql_connect("bimmercenter.com",$database-username,$database-password);
    

    3. You need to create a table in your MySQL database. E.g. your database's name is CBG_DB and your table's name is CBG_TBL.

    So you can either:
    $database-name="CBG_DB";
    mysql_select_db($database-name);
    

    or
    mysql_select_db("CBG_DB");
    


    4. field1, field2, field3 etc. are the fields you create in your table. If you created a table called CBG_TBL with 3 fields email, firstname, lastname, then:
    $q="INSERT INTO CBG_TBL (email, firstname, lastname) VALUES('$email','$firstname','$lastname')";
    

    $email, $firstname and $lastname need to be input fields of your form.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay. Thanks for all this help.
    I'll test all that now!
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
Sign In or Register to comment.