Jump to content

Recommended Posts

Hi, I don't know the first thing about coding, and need some help. I need to design a page in php that would be a simple form where the user inputs their email address into a box, with a submit button.

After submitting the email address however, I need a few things to happen:

1. Process the form data

2. Validate the E-mail address

3. Check to see if the email address is already been inputted

4. If not then continue

5. I then need to send a code from my "code table" to their email address automatically.

 

This is for an online psychology experiment with two conditions (two sets of codes) and I need to alternate between these conditions when codes are sent out. Can anyone help. I had someone from uni design the rest of the site for me, but now they won't help me do this bit. How would I go about doing this sort of thing? Thanks

Link to comment
https://forums.phpfreaks.com/topic/70628-complete-noob-needs-help/
Share on other sites

try looking in the tutorials section on this site. They've got some great tutorials on just about everything to get you started. What you're wanting to do sounds quite simple so look up some php email scripts and come back with what you've got!

Then we can help you work on it :)

 

Also try looking on php.net

a comment form would work in pretty much the same way. All you really need is the script which sends the email. The form is just a simple html form.

 

here's an example form for you:

<form action="sendmail.php" method="post">
<input type="text" maxlength="255" name="email" value="" />
<input type="submit" name="submit" value="send" />
</form>

If you don't know html I'll quickly explain what that does.

<form action="sendmail.php" method="post">
// this tells the server that the next part is a form.
The action is generally the page the form is sent to to verify the details, send email etc.
the method should usually be left as post. //

<input type="text" maxlength="255" name="email" value="" />
// this creates the input field for users to add their email address.
the maxlength="255" means that they can only enter up to 255 characters
(which I believe is the max for an email address) //

<input type="submit" name="submit" value="send" />
// this is the submit button to send the form. the value is the text displayed on the button //

then finally:

</form>
// this tells the server that this is the end of the form //

Hi, I don't know the first thing about coding, and need some help. I need to design a page in php that would be a simple form where the user inputs their email address into a box, with a submit button.

After submitting the email address however, I need a few things to happen:

1. Process the form data

2. Validate the E-mail address

3. Check to see if the email address is already been inputted

4. If not then continue

5. I then need to send a code from my "code table" to their email address automatically.

 

This is for an online psychology experiment with two conditions (two sets of codes) and I need to alternate between these conditions when codes are sent out. Can anyone help. I had someone from uni design the rest of the site for me, but now they won't help me do this bit. How would I go about doing this sort of thing? Thanks

 

The best advice I can give you is to try to design the individual steps separately, so you don't lose the forest for the trees, so to speak.

 

1. Process an e-mail address: You have a form on your site that accepts an e-mail address, correct?  If so, then this is basically done for you.

 

2. Validate the e-mail address: This can be tricky as form validation is typically done through the use of regular expressions, which is a fancy term for creating a pattern and accepting (or denying) inputed data based on that pattern.  One pattern I like to use is:

<?php 
function myEscape($string){
	return get_magic_quotes_gpc() ? mysql_real_escape_string(stripslashes($string)) : mysql_real_escape_string($string);
}

if(isset($_POST['email'])){
		if(preg_match('/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/i', $_POST['email'])){
			$email = myEscape($_POST['email']);
		}
}
.
.
.
?>

 

First, a word of warning: the code snippet above won't work 'out of the box.'  So, you can't just copy and paste it into your code and expect results.  The important parts of the code snippet are the if-conditionals.  The first says "If the email input is set with a value...."  The second says "...and if it matches this confusing pattern, then set a local variable named $email with an escaped (i.e. safe for database storage) version of that value."

 

Feel free to use that pattern.

 

3. Check to see if the e-mail address has already been inputed: This requires accessing a database or file.  I'm assuming it's a database, since that's how these things tend to work.  I'm also assuming that it's a MySQL database.  You could do something like:

<?php
   $query = "SELECT * FROM table_name WHERE email = '$email'";
   $result = mysql_query($query);

   if($result){
      //e-mail address is in the database
      //do something with it
   }
   else{
      //e-mail is NOT in the database -- insert it
      $insertQuery = "INSERT INTO table_name (email) VALUES ($email)";
      $insertResult = mysql_query($insertQuery);

      if(mysql_num_rows() == 1){
         //success!
      }
      else{
         //couldn't enter the e-mail address into the database
      }
   }
?>

 

Again, this won't work out of the box, but it should get you started.  The value for table_name is whatever the name of your particular database table is.  The lines beginning with // are where you may need to flesh things out, especially the last two.

 

4. If not, then continue: This was covered with step 3.

 

5. I then need to send a code from my "code table" to their email address automatically: Again, this isn't too difficult.  The biggest hurdle is probably selecting the right code to send from your code table, assuming they're not all the same.

 

<?php
   $codeQuery = "SELECT code FROM code_table_name WHERE some_value = '$someValue'";
   $codeResult = mysql_query($codeQuery);
   $row = mysql_fetch_assoc($codeResult);
   $code = $row['code'];

   mail($email, 'Subject goes here', $code); //$email is the e-mail address we obtained earlier
?>

 

Again, this is just supposed to give you an idea on how to proceed.  Things like $someValue and whatnot are placeholders for values in your actual system.

 

Hope this gives you some idea on how to move forward! :)

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.