AJAX/JSON and PHP Signup Form help

Discussion in 'PHP' started by ethikz, Sep 27, 2011.

  1. #1
    I have an AJAX form and after someone signs up it validates and then posts to a DB. I am trying to figure out how to email the signup as well. I have tried adding a second action on the form with no luck.

    Any help would be appreciated

    Here is the PHP
    <?php
    if($_GET['action'] == 'signup'){
    	
    	mysql_connect('localhost','dbuser','dbpass');  
    	mysql_select_db('dbname');
    	
    	$email = mysql_real_escape_string($_POST['signup-email']);
    	
    	if(empty($email)){
    		$status = "error";
    		$message = "You did not enter an email address!";
    	}
    	else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){
    			$status = "error";
    			$message = "You have entered an invalid email address!";
    	}
    	else {
    		$existingSignup = mysql_query("SELECT * FROM signups WHERE signup_email_address='$email'");   
    		if(mysql_num_rows($existingSignup) < 1){
    			
    			$date = date('Y-m-d');
    			$time = date('H:i:s');
    			$type = ($_POST['signup-type']);
    			
    			$insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time, signup_type) VALUES ('$email','$date','$time', '$type')");
    			if($insertSignup){
    				$status = "success";
    				$message = "You have been signed up!";	
    			}
    			else {
    				$status = "error";
    				$message = "Ooops, There's been a technical error!";	
    			}
    		}
    		else {
    			$status = "error";
    			$message = "This email address has already been registered!";
    		}
    	}
    	
    	$data = array(
    		'status' => $status,
    		'message' => $message
    	);
    	
    	echo json_encode($data);
    	exit;
    }
    ?>
    
    PHP:
    Here is the HTML
    <form id="newsletter-signup" action="?action=signup" method="post">
    <input type="text" name="signup-email" id="signup-email" class="fieldz" /> 
    <input type="image" src="img/submit1.png" class="submitbtn" />
    <input type="hidden" name="signup-type" value="Subject1" /></form><br /><p id="signup-response"></p>
    
    <form id="newsletter-signup2" action="?action=signup" method="post">
    <input type="text" class="fieldz" name="signup-email" id="signup-email" /> 
    <input type="image" src="img/submit2.png" class="submitbtn" />
    <input type="hidden" name="signup-type" value="Subject2" /></form><br /><p id="signup-response2"></p>
    
    HTML:

     
    ethikz, Sep 27, 2011 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    Adding an action attribute to the form wont do anything, you need to edit the PHP script.
    You need to use the mail function.
    Add this just above the
    $insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time, signup_type) VALUES ('$email','$date','$time', '$type')");
    PHP:
    part.
    It would look something like;
    mail("my@mail.com", "New signup!", "{$email} has signed up for your newsletter!");
    PHP:
     
    Sky AK47, Sep 27, 2011 IP
  3. ethikz

    ethikz Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    LOL Wow I don't know why I didn't think of this easy fix before. It always helps to have someone else look at it though.

    I did however modify it and works like a charm.

    $recipient = "email@email.com";
    			$formcontent="From: $email \nSignup Type: $type\nI would like to reserve a SPOT please";
    			$subject = "New Signup";
    			$mailheader = "From: $email \r\n";
    			mail($recipient, $subject, $formcontent, $mailheader);
    PHP:
    I did do something similar before but for some reason it kept emailing me after I would refresh the page. Oh well it works now.


    Thanks a lot for your help +rep
     
    Last edited: Sep 27, 2011
    ethikz, Sep 27, 2011 IP