How to do this

aladinhw

Registered Member
Joined
Jul 12, 2009
Messages
81
Reaction score
69
hello
I have a HTML page and I want to add this to it ( like explained in the picture ) . I can do HTML but I don't know a lot about PHP and how to connect it with MySQL. I need to put the code on my HTML page and when people press the add botton , the script test the boxes and see if the " E-mail " is real and if the other boxes are filled or not then IF everything is good , It sends the " Email " boxe , the " Number " boxe , and the " comment '' boxe to my SQL database. All what I can do is add the name and the password of the database to the script.
I really need help.
And thank you very very very much in advance

endd.jpg



I tried this but it didn't work or maybe I didn't knew how to add it to my HTML page

Code:
<? php

// Connects to your Database 
mysql_connect("Host", "user", "pass") or die(mysql_error()); 
mysql_select_db("Database") or die(mysql_error());


//This makes sure they did not leave any fields blank
if (!$_POST['email'] | !$_POST['number'] | !$_POST['comment']  ) {
die('Please Complete All the Fields.');
}

if (strpos($_POST['email'], '@') === false) {
    print 'This is not a valid email adress';
 }


if (isset($_POST['add'])) { 

// now we insert it into the database
$insert = "INSERT INTO tb_users (email, number, comment )
VALUES ('".$_POST['email']."' , '".$_POST['number']."'  , '".$_POST['comment']."'  )";

$add_member = mysql_query($insert);


}

?>

<?php 
echo("<p>Thank you for you're comment <p/>");
} 
else 
{ 
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input type="text" name="email" maxlength="20">
<input type="text" name="number" maxlength="10">
<input type="text" name="comment" cols="20" rows="10" maxlength="100">


<input type="submit" name="add" value="Add">

<?
}
?>

So , anyone can help please
 
I didn't test it.
Code can have small mistakes.
PHP:
<?php

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

/*
**check if in e-mail is "@",  check if e-mail has one of this structure: 
**[email protected]
**[email protected]
**[email protected]
*/
function email_valid($email) {
  if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
  {
    $email_err='Wrong e-mail address';
    return false;
  }
  else{$email_err='';return true;}
}


function show_form(){
echo'
<form action="" method="post">
'.$email_err.'<input type="text" name="email" maxlength="20">
<input type="text" name="number" maxlength="10">
<input type="text" name="comment" cols="20" rows="10" maxlength="100">

<input type="submit" name="add" value="Add">
';
}

	
/*main code*/

if(!empty($email) || !empty($number) || !empty($comment) || email_valid($email)){
	echo 'Please Complete All Fields';
	show_form();	
}
else
{
	mysql_connect("Host", "user", "pass") or die(mysql_error()); 
	mysql_select_db("Database") or die(mysql_error());
	$insert = "INSERT INTO tb_users (email, number, comment ) VALUES ('".$email."' , '".$number."'  , '".$comment."'  )";
	$add_member = mysql_query($insert);
	mysql_close();
}
?>
 
Back
Top