aub
-
Posts
15 -
Joined
-
Last visited
Posts posted by aub
-
-
I need to validate first name, last name, street, suburb, postcode, email, status and date of birth.
I tried to do this validation but gave me heaps of error and I messed up more.
if (empty($firstname)) {$errors[] =" First Name Can not be Empty <br> ";} if (empty($lastname)) {$errors[] =" Last Name Can not be Empty <br> ";} if (empty($street)) {$errors[] =" Street Can not be Empty <br> ";} if (empty($suburb)) {$errors[] =" Suburb Can not be Empty <br> ";} if (empty($postcode)) {$errors[] =" Postcode Can not be Empty <br> ";} // elseif (!is_numeric($postcode)) {$errors[] =" Postcode must be numeric ";} elseif(!preg_match("/\^\(\[0\-9\]\{5\}\(\[\-\\s\]\?\[0\-9\]\{4\}\)\?\)\$/", $postcode)) {$errors[] =" Please enter a valid post number <br> ";} if( !preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $myemail) ) {$errors[] =" You have entered and invalid email address <br> ";} if (empty($DOB)) {$errors[] =" Date only <br> ";}
This is my full PHP/MySQL code:
<html> <head> <style> body { background-color:#d0e4fe; } h1 { color:orange; text-align:center; } p { font-family:"Times New Roman"; font-size:20px; } table,th,td { border:1px solid black; } table { width:90%; } th { height:40px; } </style> </head> <body> <h1>All Students Information</h1> </body> </html> <?php function renderForm($first, $last, $st, $sub, $pcode, $em, $sta, $dofb, $error) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Add Student</title> </head> <body> <?php if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } echo "<p><b>Add new student details</b></p>"; ?> <form action="" method="post"> <div> <strong>First name: </strong> <input type="text" name="FNAME" value="<?php echo $first; ?>" /><br/> <strong>Last name: </strong> <input type="text" name="LNAME" value="<?php echo $last; ?>" /><br/> <strong>Street: </strong> <input type="text" name="STREET" value="<?php echo $st; ?>" /><br/> <strong>Suburb: </strong> <input type="text" name="SUBURB" value="<?php echo $sub; ?>" /><br/> <strong>Postcode: </strong> <input type="text" name="POSTCODE" value="<?php echo $pcode; ?>" /><br/> <strong>Email: </strong> <input type="text" name="EMAIL" value="<?php echo $em; ?>" /><br/> <strong>Status: </strong> <input type="text" name="STATUS" value="<?php echo $sta; ?>" /><br/> <strong>Date of Birth: </strong> <input type="text" name="DOB" value="<?php echo $dofb; ?>" /><br/> <p>Required Field</p> <input type="submit" name="submit" value="Submit"> </div> </form> <p><a href="view.php">Update & delete students</a></p> </body> </html> <?php } include('dbconnect.php'); if (isset($_POST['submit'])) { $firstname = mysql_real_escape_string(htmlspecialchars($_POST['FNAME'])); $lastname = mysql_real_escape_string(htmlspecialchars($_POST['LNAME'])); $street = mysql_real_escape_string(htmlspecialchars($_POST['STREET'])); $suburb = mysql_real_escape_string(htmlspecialchars($_POST['SUBURB'])); $postcode = mysql_real_escape_string(htmlspecialchars($_POST['POSTCODE'])); $email = mysql_real_escape_string(htmlspecialchars($_POST['EMAIL'])); $status = mysql_real_escape_string(htmlspecialchars($_POST['STATUS'])); $dob = mysql_real_escape_string(htmlspecialchars($_POST['DOB'])); if ($firstname == '' || $lastname == '' || $street == '' || $suburb == '' || $postcode == '' || $email == '' || $status == '' || $dob == '') { $error = 'Your field is empty'; renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error); } else { mysql_query("INSERT student SET FNAME='$firstname', LNAME='$lastname', STREET='$street', SUBURB='$suburb', POSTCODE='$postcode', EMAIL='$email', STATUS='$status', DOB='$dob' ") or die(mysql_error()); header("Location: view.php"); } } else { renderForm('','','','','','','','',''); } ?>
For easy view, click the link below.
http://codepaste.net/3usepxYour help would be greatly appreciated!
Thank you. -
Yes I've tried and couldn't find it :s
-
Your help would be appreciated! Thanks!
-
Design a form that accept your first name, last name and email address. Use GET for posting the form into the same php page, print out the result:
(first_name, last_name, and email_address after the form is being submitted, you must validate your first_name, last_name are alphabetic, and your email address is valid. Also all fields must be mandatory.
I don't know how to make first name and last name alphabetic and email address valid.
This is my code:
<html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> <title>PHP Scripting</title> </head> <body> <h1>TASK 3: Question 4</h1> <?php if (isset($_GET['Submit'])) { echo "inside the form". $_GET['Submit'] . "<br>"; $firstname = $_GET['fname']; $lastname = $_GET['lname']; $DOB = $_GET['dob']; if(empty($_GET['fname']) || empty($_GET['lname']) || empty($_GET['dob'])) { echo 'Please fill in all of the fields.'; } else { $firstname = $_GET['fname']; $lastname = $_GET['lname']; $dob = $_GET['dob']; if(ucfirst($firstname) != $firstname || ucfirst($lastname) != $lastname) { echo 'The first letter of your first and last name must be in upper case.'; } else { echo 'Thank you ' . htmlspecialchars($firstname) . ' ' . htmlspecialchars($lastname) . ', for entering your date of birth: ' . htmlspecialchars($dob) . '<br />'; } } } else { ?> <form action="getform.php", method="get"> <p>First Name: <input type="text" name="fname" value=""/></p> <p>Last Name: <input type="text" name="lname" value=""/></p> <p>DOB: <input type="date" name="dob" value=""/></p> <p><input type="submit" name="Submit" value="Submitted"/></p> <?php echo "not in form yet"; echo "<br>" . $_SERVER['PHP_SELF']; } ?>
-
You have a left of the semi-colon at the end of the second line
$cities = array("New York", "London", "Sydney", "Paris", "Brisbane"); // <-- missing semi-colon
Oh my friggin god! What the hell? How embarrassing! Thanks for the help! Really appreciate it.
-
Hi guys, just need your tiny help. I'm learning as you can see.
The question says:
Create a script to check if the value "Sydney" is stored in the following array (hint: use function: in_array)
$cities = array("New York", "London", "Sydney", "Paris", "Brisbane")
(Note: you must print the ending result)This is my code:
<?php $cities = array("New York", "London", "Sydney", "Paris", "Brisbane") if (in_array("Sydney", $cities)) { echo "Yes, Sydney is stored!"; } ?>
I cant fix the problem?
Your help would be lovely.Thanks!
-
Never mind, it works now :S Any mod can close this thread. Cheers! <3
-
-
Hi guys, this forum says Time Now: Apr 27 2014 06:14 AM on the bottom of the page but in Australia where I live right now, it's 4:17pm so how do I change this forum time?
Thanks!
-
For some reason, when I click the button "Submitted", the error came up "Object not found!" but I believe my php still works?
<html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> <title>Form Page: sampleform</title> </head> <body> <h1>postback.php:postback practice</h1> <?php if (isset($_POST['Submit'])) { echo "inside the form". $_POST['Submit'] . "<br>"; $firstname = $_POST['fname']; $lastname = $_POST['lname']; $DOB = $_POST['dob']; $address = $_POST['add']; $suburb = $_POST['sub']; $state = $_POST['state']; $postcode = $_POST['pcode']; $contactphone = $_POST['phone']; $myemail = $_POST['Email']; $errors = array(); $errors[]=NULL; if (empty($firstname)) { $errors[0] =" First Name Can not be Empty <br> "; } if (empty($lastname)) { $errors[1] =" Last Name Can not be Empty <br> "; } if (empty($DOB)) { $errors[2] =" date only <br> "; } if (empty($address)) { $errors[3] =" Address Can not be Empty <br> "; } if (empty($suburb)) { $errors[4] =" suburb Can not be Empty <br> "; } if (empty($state)) { $errors[5] =" state Can not be Empty <br> "; } if (empty($postcode)) { $errors[6] =" postcode Can not be Empty <br> "; } if (empty($contactphone)) { $errors[7] =" contact phone Can not be Empty <br> "; } if (empty($myemail)) { $errors[8] =" email Can not be Empty <br> "; } echo "<pre>"; var_export($errors); echo "</pre>"; //if (count($errors) > 0) { foreach ($errors as $msg) { echo "<br> $msg"; } /* if (!empty($uname) && !empty($myemail)) { echo "Thank you for your submission, one of our staffs will contact you within 24 hours <br>"; } else echo "Sorry student firstname, student lastname, student dob, streeet address, suburb, state, post code, contact phone and/or email can not be EMPTY, please resubmit <br>"; */ } else { ?> <fieldset> <legend>Personal Details:</legend> <form action="activity6.php", method="post"> <p>Student First Name: <input type="text" name="fname" value=""/></p> <p>Student Last Name: <input type="text" name="lname" value=""/></p> <p>Student DOB: <input type="date" name="dob" value=""/></p> <p>Street Address: <input type="text" name="add" value=""/></p> <p>Suburb: <input type="text" name="sub" value=""/></p> <p>State: <input type="text" name="state" value=""/></p> <p>Post Code: <input type="text" name="pcode" value=""/></p> <p>Contact phone: <input type="text" name="phone" value=""/></p> <p>Email address: <input type="text" name="Email" value=""/></p> <p><input type="submit" name="Submit" value="Submitted"/></p> </fieldset> </form> <?php echo "not in form yet"; echo "<br>" . $_SERVER['PHP_SELF']; } ?>
Thanks!
EDIT: Forgot to say this; can you let me know if it works or not. Thanks again -
I dont have a code. I just dont know how :S
-
What does it mean by table? Is it a (My)SQL Database table returned by your query?
NB: I have corrected your topic title
Yes, that's what I mean
And thanks for correcting my topic title!
-
Hi guys, this is my very first post and thread as you can see. At TAFE, I'm doing Certificate IV and we started learning how to code in php. It's pretty fun, to be honest!
I have 3 questions and I just need your help if you don't mind or if you're bored/free.
_____________________________________________________________
Having the following students table:
1. Convert the table into an array (Linear array)
2. Convert the array into associative array
3. Use list function to print the SID, and NAME, i.e key and value_____________________________________________________________
Your help would be greatly appreciated!
Thanks.
Error in PHP Code
in PHP Coding Help
Posted
You forgot to close phpURL tag?