Jump to content

[SOLVED] PHP Form


BrotherBear

Recommended Posts

hi all

 

I still have problems with my form what can I do? ::)

 

I want the PHP to check if the space has words or something and if doesn't I want the PHP script to tell people they need to fill every space I have 2 pages that do this work of sending the info to the database the first one is contactus.html wich has just the form here's the code

 

   <form action='processing.php' method='post'>
  <div class='name'>*Name:</div>
  <div class='namebox'><input type='text' name='Name' style='font-family:monotype corsiva; font-size:15px;' /><br><?echo "$error" ?></div>
  <div class='age'>*Age:</div>
  <div class='agebox'><input type='text' name='Age' style='font-family:monotype corsiva; font-size:15px;' /><br></div>
  <div class='email'>*Email:</div>
  <div class='emailbox'><input type='text' name='Email' style='font-family:monotype corsiva; font-size:15px;' /><br></div>
  
  <div class='commentss'>*Comments:</div>
  <div class='commentssbox'><textarea name='Comments'>Write Your Message</textarea></div>
  <div class='submit'><input type='submit' value='Send!' /></div>
  </form>

 

and the next code is the one that prcess the data and inserts it in the database

 

 

<?php
$con = mysql_connect("mysql1.100ws.com","johncoda_downloa","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$Name = $_POST['Name']; 
$Age = $_POST['Age']; 
$Email = $_POST['Email']; 
$Comments = $_POST['Comments']; 


mysql_select_db("johncoda_downloa", $con);


$sql = mysql_query("INSERT INTO Customers (Name, Age,  
        Email, Comments) 
        VALUES('$Name', '$Age', '$Email',  
        '$Comments')")  
        or die (mysql_error()); 
?>
<html>
<head>
<meta http-equiv="Refresh"
content="5;url=http://johncoda.freestarthost.com/thankyou.html">
</head>
<body>
</body>
</html>

 

if you need to see the error by yourself and the form problem go here http://bbanddisney.freeprohost.com/contactus.html

 

any advices to make a space checking with PHP?

 

BB

Link to comment
Share on other sites

um, from memory, look at the strip_char() function, althpough thats prolly nt its name.  thers a function that finds special characters ie "<>?,./;':"[]\{}| etc and the whitecpase, the replaces then with their something or rather name ie   which is the whitespace from memory

 

goo dluck i hope thats what you were looking for

Link to comment
Share on other sites

sorry but I didn't get quite right what you said

 

I want like

 

if

  (name has words)

  continue with script

if

  (name doesn't have anything)

  error please fill spaces

  go to form again

 

that way the php checks all spaces to make sure they have something but they let you send the info when everything is ok

 

BB

Link to comment
Share on other sites

<?php

$error = array(); //holds any errors from the form
//check name is entered
if(!isset($_POST['Name']) || strlen(trim($_POST['Name']))==0){
  $error[] = "Please enter a you name";
}
//check age is entered
if(!isset($_POST['Age']) || strlen(trim($_POST['Age'])==0){
  $error[] = "Please enter your age";
}
//check email is entered
if(!isset($_POST['Email']) || strlen(trim($_POST['Email'])==0){
  $error[] = "Please enter you email address";
} else {
  //check email is valid
  $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                 ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                 ."\.([a-z]{2,}){1}$";

  if(!eregi($regex, $_POST['Email'])){
    $error[] = "Please enter a valid email";

  }

}
//check comment is entered
if(!isset($_POST['Comments']) || strlen(trim($_POST['Comments'])==0){
  $error[] = "please enter a comment";
}
//display the errors found
if(count($error) > 0){
  echo "Errors(s) found"
  echo "<ul>"
  foreach($error as $error_item){
    echo '<li>'.$error_item.'</li>';
  }
  echo '</ul>';
} else {
  //do your db stuff
  $con = mysql_connect("mysql1.100ws.com","johncoda_downloa","******");
  if (!$con)
  {
   die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("johncoda_downloa", $con);

  $sql = sprintf("INSERT INTO Customers (Name, Age, Email, Comments) VALUES ('%s', '%s', '%s', '%s')",
             mysql_real_escape_string($_POST['Name']),
             mysql_real_escape_string($_POST['Age']),
             mysql_real_escape_string($_POST['Email']),
             mysql_real_escape_string($_POST['Comments'])
            );
  $result = mysql_query($sql) or die(mysql_error();
  if($result){
    header("Location: http://johncoda.freestarthost.com/thankyou.html");
  }
?>

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.