Jump to content

Which field to post to DB statement

- - - - -

  • Please log in to reply
2 replies to this topic

#1
ethikz

ethikz

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts
Alright I am having sort of an issue, maybe it is a brain fart. I have 2 forms, 1 for one type of person and 1 for the other type of person. What I want to do is to determine which submit button is being pressed and then submit the proper type to the DB. So far I can only get it to post only 1 type. I know I am missing some code somewhere and maybe even a conditional statement or 2 but I can't pinpoint it. Any help would be appreciated it. If you need other code just let me know.

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;

}

?>


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-signup" 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-response"></p>



Thanks in advance

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
I see 3 way to do so

#1: add a variable into the action:
<form id="newsletter-signup" action="?action=signup&form=1" method="post">...</form>

<form id="newsletter-signup" action="?action=signup&form=2" method="post">...</form>

#2 add an input type hidden in each form
<input type="hidden" name="whatForm" value="form1" />

Or #3 add a name & value to the submit button

I don't think one is better than the other, so you decide

#3
ethikz

ethikz

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts
Well to be honest I'm not sure if that would be best for what I am looking to do. It's an ajax/json form and that would add way more lines of code to validate both forms. What I am wanting to do is keep 1 form and validate the form like it does then determine which type to post to the DB (the type field doesn't need any validation, it's just a way of determining what type the person who sign's up is...the only thing being validated is the email and that isn't going to change).

Any insight? If the same answers still stand then please let me know and maybe I can pay someone to help me.


** Nevermind I fixed it..

Edited by ethikz, 26 September 2011 - 12:32 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users