PHP - Adding Php Field Check With Output
Hello there,
My current song request form process looks like: <?php if(isset($_POST['submit'])) { $to = 'address@website.com' ; $name = trim($_POST['name']); $message = trim($_POST['message']); $artist = trim($_POST['artist']); $song = trim($_POST['song']); $subject="Name:$name - Message:$message - Artist:$artist - Song:$song"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $message = "<table> <tr><td>Name</td><td>".$_POST['name']."</td></tr> <tr><td>Message</td><td>".$_POST['message']."</td></tr> <tr><td>Artist</td><td>".$_POST['artist']."</td></tr> <tr><td>Song</td><td>".$_POST['song']."</td></tr> </tr></table>" ; mail($to, $subject, $message, $headers); header('Location: song-requests-success.php'); } ?> I'm currently using Javascript for blank validation, however, people seem to be getting through it somehow. I'm looking to implement PHP checkers that will output back to the request form with something like: Name field blank. Would this method work? $name = trim($_POST['name']); $artist = trim($_POST['artist']); $song = trim($_POST['song']); if(empty($name)){ $name1 .= "<br>Name is empty."; } if(empty($artist)){ $artist1 .= "<br>Artist is empty."; } if(empty($song)){ $song1 .= "<br>Song is empty."; } On form: <?php if(!empty($name)){ echo "".($name1).""; } ?> <?php if(!empty($artist)){ echo "".($artist1).""; } ?> <?php if(!empty($song)){ echo "".($song1).""; } ?> Would recalling the trim's be nessessary as they have already been specified for the email title. Would this need to be implemented into 'isset'. Many thanks. Similar Tutorials(Another quite newbie...) I have already an online booking form. The Form works fine now and I would like to add on one more issue, but the code ignores what I want to check. I have 4 fields: arrival, departure, no. of persons and comments to check. Scenario 1: All field mentioned above are emtpty: Workes fine and message appears: "You have not provided any booking details". Scenario 2: If arrival (date_start) and departure (date_end) is entered, there should be at least an entry either in the field "comment", or in the field "pax". If not, there should be a message: "You have not provided sufficient booking details". INSTEAD: The booking request is sent, which should not be the case. The code is currently: # all fields empty : arrival, departure, pax and comments - error if(empty($data_start) && empty($data_end) && empty($pax)&& empty($comment)){ exit("You have not specified any booking details to submit to us. <br>Please use your browser to go back to the form. <br>If you experience problems with this Form, please e-mail us"); exit; } #If arrival and departure date is entered, there should be at least an entry either in the field "comment", or in the field "pax". if(!isset($data_start) && !isset($data_end) && empty($pax) && empty($comment)){ exit("You have not provided sufficient booking details. <br>Please use your browser to go back to the form. <br>If you experience problems with this Form, please e-mail us"); exit; } The form can be tested at http://www.phuket-beachvillas.com/contact-own/contact-it.php Can someone please check and tell me what's wrong with the code ? Thanks to anyone for any hint. Hello, Having trouble inserting plain text to the output of this php. The code below outputs categories in the database which pertain to certain products in this manner, cat1, cat2, cat3, cat4, cat5 etc. What I want to add is the text "Fits These Products" this to the output, Fits These Products cat1, cat2, cat3, cat4, cat5 etc Code: [Select] $lc_align = ''; if(isset($HTTP_GET_VARS['language'])) { $env_language = $HTTP_GET_VARS['language']; $language_query = tep_db_query("select languages_id from ". TABLE_LANGUAGES . " where code ='" . $env_language . "'"); $language_arr = tep_db_fetch_array($language_query); $language_id = $language_arr['languages_id']; } else { $language_id = (int)$languages_id; } $products_to_catagories_query = tep_db_query("SELECT categories_name FROM ". TABLE_PRODUCTS_TO_CATEGORIES ." p2c, ". TABLE_CATEGORIES_DESCRIPTION ." cd WHERE p2c.products_id = " . (int)$listing[$x]['products_id'] . " and p2c.categories_id = cd.categories_id and language_id ='".(int)$language_id."'"); $category_list = " "; do { if($category_list != " ") $category_list .=" , "; else $products_to_categories_array = tep_db_fetch_array($products_to_catagories_query); $category_list .= $products_to_categories_array['categories_name']; } while($products_to_categories_array = tep_db_fetch_array($products_to_catagories_query)); $category_list .="<br><br>"; Thank you JR I currently have php code that outputs information from my database, but how do i make it also display the name of the fields beside each entry of the database? heres my PHP code: Code: [Select] <?php $server = ""; // Enter your MYSQL server name/address between quotes $username = ""; // Your MYSQL username between quotes $password = ""; // Your MYSQL password between quotes $database = ""; // Your MYSQL database between quotes $con = mysql_connect($server, $username, $password); // Connect to the database if(!$con) { die('Could not connect: ' . mysql_error()); } // If connection failed, stop and display error mysql_select_db($database, $con); // Select database to use // Query database $result = mysql_query("SELECT * FROM Properties"); if (!$result) { echo "Error running query:<br>"; trigger_error(mysql_error()); } elseif(!mysql_num_rows($result)) { // no records found by query. echo "No records found"; } else { $i = 0; echo '<div style="font-family:helvetica; font-size:15px; padding-left:15px; padding-top:20px;">'; while($row = mysql_fetch_array($result)) { // Loop through results $i++; echo '<img class="image1" src="'. $row['images'] .'" />'; //image echo "Displaying record $i<br>\n"; echo "<b>" . $row['id'] . "</b><br>\n"; // Where 'id' is the column/field title in the database echo $row['Location'] . "<br>\n"; // Where 'location' is the column/field title in the database echo $row['Property_type'] . "<br>\n"; // as above echo $row['Number_of_bedrooms'] . "<br>\n"; // .. echo $row['Purchase_type'] . "<br>\n"; // .. echo $row['Price_range'] . "<br>\n"; // .. } echo '</div>'; } mysql_close($con); // Close the connection to the database after results, not before. ?> thanks in advance Hey all. What is the best way to check if a form field has been entered by the user? Because a field left blank by the user still shows up as set with the isset function. Example: if (isset($_POST['name'])) { echo "The field is set"; } This is a problem if I'm checking if the user has skipped over the name field on a form because an empty value gets passed to the POST array even if the field is left blank. Do people use empty or a regular expression instead? Cheers! I have a code which I made just up Code: [Select] <?php if ($product->title == '01'); print "Earring"; ?> <?php if ($product->title == '02'); print "Necklace"; ?> <?php if ($product->title == '03'); print "Necklace"; ?> <?php if ($product->title == '04'); print "Ring"; ?>I just want to check the product titles first 2 characters and print according to that. Example: If $product->title is "01DSE32" print Earring. This checking must be strgn or something like that which I don't remember. I would appreciate some feedback. Ok so i have registration page on my webs but i didn't work on empty fields so how could i check all fields? Code: [Select] //if no name if(!$name){ $error = "Username missing."; } //if no pass if($pass == false){ $error = "$error,Password missing"; } //if no pass conf if($pass_conf == false) { $error = "$error,Pass conf missing."; } //if no email if($email == false){ $error = "$error,Email missing."; } //pass conf if($pass != $pass_conf){ $error = "$error,Passwords do not match."; } echo "<script>alert('$error');</script>;"; //header("Location: register.php"); My script gives same results every time (even if i fill any of fields)...also if i un-comment header command it wont show alert..it just reloads the page So can anyone help me with this? Thanks.. I have this script running: <?php $query = mysql_query("SELECT * FROM jobs WHERE event = 'Yes' ORDER BY title"); while ($row = mysql_fetch_array($query)) { ?> How do I check if a field is empty and NOT display it...? For instance it has a 'applied' field, if that is empty I dont want it to display, however I still need the event = 'Yes' part. Hey All, first off Merry X-Mas ok so I'm trying to insert something into the batabse in one of three areas' let me explain there are three fields in the users database bounceone, bouncetwo and bouncethree now what I want to do is when the user clicks add to bounce list insert a value into the databse in one of these three hole, if bounceone is empty put it there, if not check bouncetwo if it's empty put it there, if not check the final one and if it's empty put it there else echo sorry please either remove a bounce any ideas on how this can be done. Hello everybody,
i try to check if the unique field inventar exists or not before i insert new record.
if the inventar exits the user should get error message.
but i dont know how to do this.
thank you very much for your help.
Rafal
here is my code
<?php include("123.php"); $inventar = $link1->real_escape_string($_POST["inp_inventar"]); $product = $link1->real_escape_string($_POST["inp_product"]); $price = $link1->real_escape_string($_POST["inp_price"]); $link1 = new mysqli("$hoster", "$nameuser", "$password", "$basedata") or die ("Connection Error!"); error_reporting (0); $check_inventar = mysqli_query($link1, "SELECT inventar FROM products WHERE inventar='$inventar'"); $check_inventar_num = mysqli_num_rows($check_inventar); if (isset($_POST['inp_submit']) AND $check_inventar_num = 0) { $query1 = "INSERT INTO products (inventar, product, price) VALUES ('$inventar', '$product', '$price')"; $result1 = $link1->query($query1) or die("Database Error!"); if ($result1 == true) { header ( 'Location:insert.php' ); } } else { echo "there is already product with same inventar id!"; } mysqli_close($link1); ?> I am building a system where people are able to choose a food menu, they can modify each dish if needed and then say how many of each item they need for the order. So when they pick the breakfast menu, it selects all starters, mains and desserts. Should something need to be edited (sausages changed to vegitarian sausages) the user should be able to overtype what has been populated from the select statement) When the form is submitted, the order needs to be linked to the current job which is in a field ssm_job.job_id but the changes to the menu items need to be stored somewhere for the current order only. This obviously needs to be accessile later so further changes can be made if needed. I am not sure of the best way to go about this as i am trying not to save every item in another database (just trying to be tidy) Any advise on this would be greatly appreciated. I have had help on this project in relation to an sql and the background can be found below, the help has been amazing but as this is a very different question i thought i would start a new thread.
Kind Regards Hi there once again, sry for previous wrong section, i guess this time i'm on the right place ;p Well i need following: I have few forms (input fields) with requested informations, also i have other .php file which i'm calling to send a form. Now i need to know how can i do a check for those fields if they are empty or not. If they are, i need to STOP submiting the form (calling the other .php). Codes: Form: Code: [Select] <form name="f1" form method="post" action="posalji.php"/>Field: Code: [Select] <input type="text" name="hp1" size="20"/>Submit: Code: [Select] <input name="f1" type="image" src="registruj.png" onclick="if(f1.hp1.value.length==0) alert('Error: field can't be empty'); else f1.submit(); if(f1.hp2.value.length==0) alert('Error: field can't be empty'); else f1.submit();"/>There are more than one field, that's why you can see more errors in submit. Well all i need is "die" or "stop" calling the "posalji.php" from action above. Sry 4 poor english. so I have this code: Code: [Select] <input type='checkbox' name='title8' value='$title8' /></td><td><strong>$title8</strong> <br /> $date8 <br />$ $price8<input type='hidden' name='price8' value='$price8'> how do I make is so that the hidden field is only going to submit if the checkbox is checked........ Pehaps this is kidstuff but i'm a kid at heart. I have only 1 form field on a form User enters a special code in the form. Once submitted the form is queried against a mysql database to see if the code exists. If code does not exist it must be reentered. If code exists then redirected. basically its like "get to the next page if you have the code" thanks I have a form with a gender control made up of two radio buttons. If the user doesn't fill out this form, nothing gets submitted (i.e name='gender') in the $_POST array. So how ae you supposed to verify that the form field was left blank, and use that information to say display an error message on the form? Edited February 16 by SaranacLakeNow this one is a bit of a shit head. I am using a really comprehensive class i found on on the web for upload / re-size / crop but i want to be able to override it's error handling and i am not good enough to start modifying the code in the class with out fucking it up. Firstly i will show you how i have invoked the class. Code: [Select] if( isset($_FILES['image'] ) ) { //Class includes include("../scrips/php/cms/database.insert.class.php"); include ("../scrips/php/cms/img.upload.resize.crop.class.php.php"); //----------|Start: upload, resize & save $your_image = new _image; //----------| Upload orginal image $your_image->uploadTo = 'uploads/'; $upload = $your_image->upload($_FILES['image']); //----------| Resize and upload small thumbnail $your_image->newPath = 'thums/'; $your_image->newWidth = 50; $your_image->newHeight = 50; $resized = $your_image->resize(); //----------| Resize and upload medium thumbnail $your_image->newPath = 'thums2/'; $your_image->newWidth = 100; $your_image->newHeight = 100; $resized = $your_image->resize(); //----------| Getting the image name to insert into the database futher on in the code $name = $resized; $img_str = explode("/",$name); $final_img_name = $img_str['1']; echo "Article has been added!"; //----------------|end //----------------| Start database insert (class manipulation) $table = "blog_posts"; $title = "'".$_POST['ArticleTitle']."',"; $img = "'".$final_img_name."',"; $post = "'".$_POST['ArticleBody']."',"; $aurthor_id = "'1',"; $category_id = "'".$_POST['Category']."',"; $date_posted = "NOW()"; $values = array("$title","$img","$post","$aurthor_id","$category_id","$date_posted"); $fields = array('title,','img,','post,','aurthor_id,','category_id,','date_posted'); $obj= new DatabaseInsert; $obj->DatabaseConnectionRequire(); $obj->ArticleInsert($values,$fields,$table); //----------------|end } As you can see it's fairly basic. What i want to do is before it runs this script and starts including/invoking the class etc, i want to be able to check to see if there is a value been posted from a FILE FORM OBJECT and if there is to proceed with this script, alternatively i want it to execute another code which will handle it in regards to the concept of my page. A simple javascript alert with be ok providing it reloaded the page to it's default state. If any one can help me here i would appreciate it a lot. Thanks Hello, I'm currently using this code to check for blank fields: if(isset($_POST['submit'])) { $emailconfirm = $_POST['emailconfirm']; $user_message = $_POST['message']; $name = $_POST['name']; $visitor_email = $_POST['email']; if(empty($name)|| empty($visitor_email)|| empty($user_message)) { $errors .= "<div id='contact_form_errorloc' class='err1'>Some of the above fields have not been filled in.</div><br>"; } This however, does not check for valid emails, I've found this code: function is_valid_email($email) { $result = TRUE; if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) { $result = FALSE; } return $result; } My question was if the above is possible aswell as incorporating the blank field checks. Many thanks! I have a register script, and I am wanting to make it so that if the username field contains, lets say "mod", "ass", and more, then it'll return an error and wont let them register. Following from my previous post, i have a questionnaire and within each question it has the options to pick from raddio button (strongly agree, agree, disagree, N/A). For each value of each question i want to pass it into the relevant Field in my database but rather than overwriting the current value i want to be added on to it. (For example if the current value is 2, once the new value (1) has added it will make the new value of 3.). Hiya pplz. I am trying to add: class="keyboardInput" to activate a javascript script to an input box. This is the line im trying to add it to: <td><?php echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));?></td> I have tried: <td><?php echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));? class="keyboardInput"></td> didn't work. Any help would be great. Ian |