Jump to content

Msgbox for form validation and MYSQL connection


marmite

Recommended Posts

Hello,

 

Complete newbie here...

 

I have a form on my website into which users can enter their email address. The form needs to validate the email is ok, replying with one msgbox if the format is invalid, and another msgbox ("Thanks!") plus an SQL command if the format is valid.

 

I am struggling to know where to put the SQL.

 

This is my setup: in between <head><script language = "javascript"> ... </script></head> I have:

 

function validate(formCheck)

{

var mail = formCheck.email.value

if (mail.indexOf("@.") == -1)

{

alert("Please type a valid email address");

formCheck.email.focus();

return false;

}

 

return true;

}

 

Then, within the <body> ... </body> part, I have:

 

<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>">

<label><img src = "buttons/free_news.gif" > <br />

  </label>

<input name="email" type="text" id="email" onfocus="this.value = ''; this.onfocus = function () {}" value="E-mail address" size="15">

</label>

  <input type="submit" name="submit" value="GO!" onclick="if (validate(form1)== true) {

$email=trim($_POST['email']);

$query="INSERT INTO email (email_address) VALUES ('$email')";

$result=mysql_query($query) or die ("Error in query: $query. " . mysql_error());

  }

  ">

</form>

 

I have tried placing the "$email...mysql_error());" within <?php ... ?> etc and all of the code beneath the form disappears!

 

So: I know that the individual elements work but I don't think I have them in the right place with the right tags. I also tried placing the SQL into the Javascript function but that didn't work either.

 

I have spent all day on this and I know it must be really simple. Can anyone help?

 

Emma

Link to comment
Share on other sites

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.