Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Well, you changed from PHP logic to do the validation to just JavaScript. Doing validation on the client (i.e. JavaScript) is optional. But, you must perform validation on the server (i.e. PHP). The reason is it is a very simple process for someone to sidestep JavaScript validation and submit any data they want to the server. If you don't have server-side validation a person could potentially submit data that could compromise your application.
-
Some other things. You can't set the value of a select list in the <select> tag. You need to set the appropriate option to selected. Also, you should add logic in the validation code to verify that the value passed for a select filed is an acceptable value. You can create an array for the select list values and use that to 1) create the select lists (with the selected option) and 2) for validating the passed value. 2. Don't put a <span> inside a select field. Put it before the opening <select> tag or after the closing </select> tag. And, don't forget to use the closing select tag! 3. You wouldn't use the same logic to set the value of a checkbox. A checkbox is either checked or not. It is different from other form fields in that the field is only passed in the post data if it was checked. So, you would set the checked status based on whether the field was passed or not. 4. Don't use checkboxes for multiple options when the user is only supposed to select one value. Use a radio group instead. Giving them the same name won't even work (unless you make them an array, but that's for a different purpose). I made a slight change to the workflow of the code to simplify things. Instead of setting the field values int he validation logic, you can instead set them at the top of the page based upon whether the values were posted or not. This way, the code in the form to set the values can just use a simple echo instead of an if() statement. I also fixed some of the issues above. But, I want you to make an attempt at creating an array for the select lists and creating code to 1) create the select lists and 2) use the array to validate the posted values. Create the arrays so the value is the key and the label is the value. You may be using the same for both, but that is not typically the case. E.g.: $breeds = array( 'PitBull' => 'PitBull', 'Lab' => 'Lab', 'Chihuaua' => 'Chihuaua' ); Revised code <?php //Create variables for form validation tracking $errors = array(); $fields = array(); //Set field values based on posted data (if sent) $fields['FirstName'] = isset($_POST['FirstName']) ? trim($_POST['FirstName']) : ''; $fields['LastName'] = isset($_POST['LastName']) ? trim($_POST['LastName']) : ''; $fields['Address'] = isset($_POST['Address']) ? trim($_POST['Address']) : ''; $fields['City'] = isset($_POST['City']) ? trim($_POST['City']) : ''; $fields['State'] = isset($_POST['State']) ? trim($_POST['State']) : ''; $fields['Zip'] = isset($_POST['Zip']) ? trim($_POST['Zip']) : ''; $fields['PhoneNumber'] = isset($_POST['PhoneNumber']) ? trim($_POST['PhoneNumber']) : ''; $fields['Email'] = isset($_POST['Email']) ? trim($_POST['Email']) : ''; $fields['PetType'] = isset($_POST['PetType']) ? trim($_POST['PetType']) : ''; $fields['Breed'] = isset($_POST['Breed']) ? trim($_POST['Breed']) : ''; $fields['PetName'] = isset($_POST['PetName']) ? trim($_POST['PetName']) : ''; $fields['NeuteredOrSpayed'] = isset($_POST['NeuteredOrSpayed']) ? trim($_POST['NeuteredOrSpayed']) : ''; $fields['PetBirthday'] = isset($_POST['PetBirthday']) ? trim($_POST['PetBirthday']) : ''; //Check if form was posted if ($_SERVER["REQUEST_METHOD"] == "POST") { //Validate required values if(empty($fields['FirstName'])) { $errors['FirstName'] = 'First name is required'; } if(empty($fields['LastName'])) { $errors['LastName'] = 'Last name is required'; } if(empty($fields['Address'])) { $errors['Address'] = 'Address is required'; } if(empty($fields['City'])) { $errors['City'] = 'City is required'; } if(empty($fields['State'])) { $errors['State'] = 'State is required'; } if(empty($fields['Zip'])) { $errors['Zip'] = 'Zip is required'; } if(empty($fields['PhoneNumber'])) { $errors['PhoneNumber'] = 'PhoneNumber is required'; } if(empty($fields['PetType'])) { $errors['PetType'] = 'Select type of pet'; } if(empty($fields['PetName'])) { $errors['PetName'] = 'PetName is required'; } if(empty($fields['PetBirthday'])) { $errors['PetBirthday'] = 'Pet Birthday is optional'; } //Check if there were errors if(!count($errors)) { //Add logic to process the form data //Include or redirect to success page echo "Validation passed."; //Temporary message for debugging purposes exit(); } } ?> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Sandy's Pet Shop</title> <link href="style.css" rel="stylesheet" type="text/css"> <div id="container"> <?php require 'header.php'; ?> </div> </head> <body> <div> <h3 align="center">Our Services</h3> </div> <div> <p> We provide hands-on care and a variety of dog grooming services. First, choose a bath or full groom, then customize your dog's salon experience with a personalized add-on package. We also offer walk-in services that are available without an appointment. Bring your dog into our salon for a free consultation! Our academy-trained groomers will personally meet with you to discuss your dog's specific needs. They'll also recommend just the right grooming service. Book an appointment today!</p> </div> <div> <form method="post"id="form" action="submitform.php"> <input type="hidden" name="submitted" value="true"/> <table border="1"> <tbody> <tr> <td>First Name:</td> <td><input name="FirstName" type="text" size="20" value="<?php echo $fields['FirstName']; ?>"> <span class="error">* Required <?php if(isset($error['FirstName']) { echo $error['FirstName']; } ?></span> </td> </tr> <tr> <td>Last Name:</td> <td><input name="LastName" type="text" size="20" value="<?php echo $fields['FirstName']; ?>"> <span class="error">* Required <?php if(isset($error['LastName']) { echo $error['LastName']; } ?></span> </td> </tr> <tr> <td>Address:</td> <td><input name="Address" type="text" size="20" value="<?php echo $fields['Address']; ?>"> <span class="error">* Required <?php if(isset($error['Address']) { echo $error['Address']; } ?></span> </td> </tr> <tr> <td>City:</td> <td><input name="City" type="text" size="20" value="<?php echo $fields['State']; ?>"> <span class="error">* Required <?php if(isset($error['City']) { echo $error['City']; } ?></span> </td> </tr> <tr> <td>State:</td> <td><input name="State" type="text" size="15" value="<?php echo $fields['State']; ?>"> <span class="error">* Required <?php if(isset($error['State']) { echo $error['State']; } ?></span> </td> </tr> <tr> <td>Zipcode:</td> <td><input name="Zip" type="text" size="5" value="<?php echo $fields['Zip']; ?>"> <span class="error">* Required <?php if(isset($error['Zip']) { echo $error['Zip']; } ?></span> </td> </tr> <tr> <td>Phone Number:</td> <td><td><input name="PhoneNumber" type="text" size="5" value="<?php echo $fields['PhoneNumber']; ?>"> <span class="error">* Required <?php if(isset($error['PhoneNumber']) { echo $error['PhoneNumber']; } ?></span> </td> </td> </tr> <tr> <td>Email Address:</td> <td><input name="Email" type="text" size="50"value="<?php echo $fields['Email']; ?>"></td> </tr> <tr> <td>Pet Type:</td> <td> <select name="PetType"> <option value="0">Please select</option> <option value="Cat">Cat</option> <option value="Dog">Dog</option> </select> <span class="error">* Required value="<?php if(isset($error['PetType']) { echo $error['PetType']; } ?></span> </td> </tr> <tr> <td>Dog Breed:</td> <td> <select name="Breed"> <option value="0">Please select</option> <option value="PitBull">Pit Bull</option> <option value="Lab">Lab</option> <option value="Chihuaua">Chihuaua</option> <option value="Terrier">Terrier</option> <option value="German Shepherd">German Shepherd</option> <option value="Other">Other</option> </select> </td> </tr> <tr> <td>Pet Name:</td> <td><input name="PetName" type="text" size="15" value="<?php echo $fields['PetName']; ?>"> <span class="error">* Required <?php if(isset($error['PetName']) { echo $error['PetName']; } ?></span> </td> </tr> <tr> <td>Spayed/Neutered:</td> <td> Yes<input name="NeuteredOrSpayed" type="radio" value="1" <?php if($fields['NeuteredOrSpayed']=='1') { echo 'checked="checked"' } ?>> No<input name="NeuteredOrSpayed" type="radio" value="0" <?php if($fields['NeuteredOrSpayed']=='0') { echo 'checked="checked"' } ?>> </td> </tr> <tr> <td>Pet's Birthday:</td> <td><input name="PetBirthday" type="text" value="<?php echo $fields['PetBirthday']; ?>"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit" name="submit"</input> </td> </tr> </tbody> </table> </form> </body> <hr> <footer> <?php require 'footer.php'; ?> </div> </footer> </body> </html>
-
What is this? //Create variables for form validation tracking $errors = array($firstnameErr, $lastnameErr, $addressErr, $cityErr, $stateErr, $zipError, $phonenumberErr, $emailErr, $pettypeErr, $breedErr, $petnameErr, $neuteredorspayedErr, $petbirthday); $fields = array($firstname, $lastname, $address, $city, $state, $zip, $phonenumber, $email, $pettype, $breed, $petname, $neuteredorspayed, $petbirthday); Those variables you are putting into the arrays don't exist. You are only creating an array with a bunch of NULL elements that have no purpose. That wasn't what I posted, so I don't know why you did that. The code for the validation logic will add the necessary elements to the arrays. In fact, doing that with the $errors array will only make the code fail validation every single time since count($errors) will always be > 0
-
Plus, that if() condition isn't doign anything. At least add the curly braces for the code-block that you will eventually add if(!count($errors)) { //No validation errors, process the data } Lastly, use COMMENTS! You will save hours of time in the future when you have to debug code. Even for the validation code I would add a comment for each field with something such as //Validate that last name is not empty or //Get neutered or spayed value, no validation It may seem like wasted content, but it isn't. You likely copy/pasted code for each field. If you accidentally leave out validation for a field or add it for a field you may come back later and scratch your head as to why you did it. If you added a comment as to what you are trying to do it will be obvious that it was a mistake.
-
Submit form values to MySQl database via AJAX and PHP
Psycho replied to helloworld001's topic in PHP Coding Help
Just so we are clear, you need an individual form for each record. It looks like you can simplify how the data is passed. Instead of creating parametized values in a url format, you can do something like this data: { name: "John", location: "Boston" } http://api.jquery.com/jquery.ajax/ -
The way I set it up, you would still want the code to define the values for the fields $fields['OptionalField'] = isset($_POST['OptionalField']) ? trim($_POST['OptionalField']) : ''; That is needed to repopulate the form with the submitted values when an error does occur.
-
Submit form values to MySQl database via AJAX and PHP
Psycho replied to helloworld001's topic in PHP Coding Help
In the first script you are overwriting the session values in the loop for each record. Therefore, when the page is done loading those session values only contain the values from the last record. Also, in the processing.php page I don't see any code that actually uses the $_POST data - only session data. So, when the processing page runs you are only getting the ID of the last record from your result set (regardless of which form you post) and the message data isn't being used. You should use hidden fields in each form for the values that apply to each record. Then, on the processing page, use the POST data to insert the records. -
First off, don't just use empty() on the raw $_POST value. A user could just enter spaces into that field and it would pass the validation. Typically, you will want to use trim() on the value (to remove leading trailing white-space characters) before checking if it is empty. The process you have is setting individual error variables for each field. That's pretty laborious. Plus, you would have to check ALL those variables individually. A better approach is to use an array to hold all the errors. If the array is empty then there are not errors. But, you aren't even checking those error variables later on. Otherwise the format of the code would work with the addition to check the errors. Here is a basic framework to follow <?php //Create variables for form validation tracking $fields = array(); //Array to hold posted values to repopulate form $errors = array(); //Array to hold errors //Check if form was posted if($_SERVER['REQUEST_METHOD']=='POST') { //Validate POST values $fields['FirstName'] = isset($_POST['FirstName']) ? trim($_POST['FirstName']) : ''; if(empty($fields['FirstName'])) { $errors['FirstName'] = 'First name is required'; } //Check if there were errors if(!count{$errors)) { //There were no errors, process the data //After processing the data include or redirect to a success page //Exit this script to prevent the form from displaying agan exit(); } } //Include form at bottom of script, add logic to re-popuated posted values and to display errors if they exist ?> <tr> <td>First Name:</td> <td><input name="FirstName" type="text" size="20" value="<?php if(isset($fields['FirstName']) { echo $fields['FirstName']; } ?>"> <span class="error">* Required <?php if(isset($error['FirstName']) { echo $error['FirstName']; } ?></span> </td> </tr>
-
Well, what are you trying to do? You say it didn't work, but we don't know what that means. Did you get an error (if so, what) or did you not get the results you expected? Since you didn't state what you are trying to accomplish we can't state whether the results are correct or not based on the code you wrote or if the code needs to change. But, I can say that the code is not right, just not sure how to modify it to get what you want. What would be helpful, is to show the sample content that you are working with and what you want that result to be.
-
That code looks to be incomplete. There isn't a complete form (there's no opening FORM tag). So, if the form fields are being sent, they are likely being sent as GET variables since that is the default method unless the method is set to POST in the opening FORM tag. Also, why are you storing the username in the session? You are then running a query to get the User ID each time the page is loaded. You should be storing the User ID in the session if you are going to need that on a frequent basis.
-
Did you look at the code I posted?
-
At the bottom is some sample code of how I would do this. It should create a resulting array in this format array ( 'domain1' => array ( 'http://page1.htm', 'http://page2.htm' ) 'domain2' => array ( 'http://page1.htm', 'http://page2.htm', 'http://page4.htm' ) 'domain3' => array ( 'http://page2.htm' ) 'domain4' => array ( 'http://page1.htm', 'http://page2.htm' ) 'domain1' => array ( 'http://page2.htm', 'http://page4.htm' ) 'domain1' => array ( 'http://page1.htm', 'http://page2.htm' ) ) You can determine which domains exists on multiple pages by the number of child array elements for each domain //Array of unique pages $pages = array('http://page1.htm', 'http://page2.htm', 'http://page3.htm', 'http://page4.htm'); //Array to hold results $domainsPages = array(); //Iterate over each page foreach($pages as $page) { //Get the HTML and the links for each page $html = file_get_html($url); $pageLinks = $html->find('a'); //Iterate over the page links foreach($pageLinks as $link) { //Get the href of the link $href = strtolower($link->href); $domain = parse_url($href, PHP_URL_HOST); //If the current page doesn't exist in //the results for the current domain - add it if(!in_array($page, $domainsPages[$page])) { $domainsPages[$domain][] = $page; } } } //Debug code foreach($domainsPages as $domain => $pagesAry) { echo "The domain '{$domain}' exists on " . count($domainsPages[$domain]) . "<br>\n"; }
- 8 replies
-
- simple html dom
- web scraper
-
(and 1 more)
Tagged with:
-
Off topic: Instead of creating a custom process to determine the domain of a URL, use the PHP function parse_url(). On Topic: Since you only be concerned with the number of instances for each domain, why not just add all the domains to an array as individual elements - THEN, when you are done processing all the pages use array_count_values(). You can then iterate over the resulting array to determine which domains are used multiple times. However, one thing isn't clear. Are you only interested in domains used in multiple pages? What if a domain is used more than once on the same page? Note: be sure to set the domain to lowercase (or uppercase) before trying to determine if the value is unique or not.
- 8 replies
-
- 1
-
- simple html dom
- web scraper
-
(and 1 more)
Tagged with:
-
1. You should not use checkboxes. Typically, checkboxes should be independent of one another - i.e. user could select one, both or neither. You should either use a select list or a radio group. 2. Don't put JavaScript code in the body of your page - put it in the head 3. Try to avoid using tables for layout. It may be quicker in the short-run. But, it is not the right way to create the page layout and will eventually cause problems. For example, in this case you need to apply the show/hide logic to a table row, but you didn't put all of the elements into a table row (HTML is not correctly formatted) <html> <head> <script type="text/javascript"> function showHide(type) { if(type=='dog') { var displayVal = ''; } else { var displayVal = 'none'; } document.getElementById('breed_row').style.display = displayVal; } </script> </head> <body> <table> <tr> <td>Pet Type:</td> <td> <input type="radio" name="type" id="cat" value="cat" onclick="showHide(this.value)">Cat <input type="radio" name="type" id="dog" value="dog" onclick="showHide(this.value)">Dog </td> </tr> <tr id="breed_row" style="display:none;"> <td>Dog Breed:</td> <td> <select name="dog_breed"> <option value="0">Please select</option> <option value="PitBull">Pit Bull</option> <option value="Lab">Lab</option> <option value="Chihuaua">Chihuaua</option> <option value="Terrier">Terrier</option> <option value="German Shepherd">German Shepherd</option> <option value="Other">Other</option> </select> </td> </table> </body> </html>
-
What are you trying to accomplish by creating a pattern? Are you trying to dynamically determine which folder to download files from? If so, you can (pretty sure) download the folder list and compare the names to do that. I've not used FTP functions in PHP enough to know. But, in determining the pattern, I assume you want to look for a specific data based on the current day's date. It appears you want to always use a Sunday date. So, how do want to choose "which" Sunday to use? I assume if it is Monday to Saturday, you want to use the next Sunday. But, what if you run the code on a Sunday? Should it use the current date or the next week's Sunday?
-
Take a look at this thread from earlier today: http://forums.phpfreaks.com/topic/294825-date-picker/ The same type of logic would work, but in this case you would trigger the action with the onchange event of the first select list and then check the selected value. However, if you need to dynamically change the options available in the second select list based on the first select list you can use a class created by one of the Gurus on this site: http://www.phpclasses.org/package/1637-PHP-Generate-dynamically-linked-select-inputs.html Although, if you will only ever have Dog & Cat, you could just have two separate breed select list and hide/display both based on the option selected.
-
Where's the code to build the page?
-
Sessions would normally be the way to go for 'cart' type data. I would almost always put the cart data in it's own subkey of the session array that way you can keep it separate from other session data you may want to keep. To debug your problem run this at the top of the page echo "<pre>" . print_r($_SESSION, 1) . "</pre>"; That will verify the values inside session before you run any logic on them.
-
You are asking us to basically instruct you in how to build a PHP/MySQL website. There is way too much to try and disseminate and we have no idea what your current level of knowledge is (seems to be you may understand HTML some, but not much else?). There is no preexisting code to show you as each application is unique. Plus, there are several different technologies involved. It sounds as a fairly simple project though. Someone may be willing to pick this up as a freelance job for not a lot of money and you could make it part of the deal that they provide instruction on the code so you can learn it as well. Otherwise, you will need to learn how to do each skill needed. HTML, CSS, JavaScript, PHP, MySQL, file management, etc., etc. None of these are extremely hard, but you can't learn them overnight (or through a forum post). If security is of any concern, you definitely don't want to do this yourself the first time out. Having said all that here is a very brief explanation of how I think it would be accomplished: 1. Determine he database structure, At a minimum, you would want two tables. One table would hod the information about the models: name, age, height, etc. It would also include a unique database ID field for each model. The second table would be for the images. There would be one record for each image (file path) with an association back to the model record. You *may* have a need to have a primary image that always shows at the top, that would just need a separate column in that table. 2. Once you have the database, you can populate it manually with just a handful of records. Then, create a PHP page to display the data for a model. It will require that the ID be passed on the query string mysite.com/show_model?id=5. That page will get the model ID when the page is accessed, query the database for the model information and the images, then build the HTML page. you would probably want a page to list models (with links to the page to display the model details). And, you may also need another page to search for models. 3. Once you have the above working with the manually entered data, you would want o build pages to add/update/delete models and images associated with models. But, the first two will take you quite a while to complete if you are learning. So, I'm not going to go into details here. I think you need to pull out a sheet of paper and write out what the workflow will be (i.e. what page do you need other than what is above or are those applicable). Then you will have an understanding of the scope of the work. You can also use that to get an estimate if needed.
-
How can I make this script refresh with ajax whenever a table updates ?
Psycho replied to Padgoi's topic in Javascript Help
Padgoi, what specifically are you wanting? This forum is for people to get help with code they have written. It doesn't look like you've even made an attempt based on what you posted. A question such as how do I update a page with AJAX is not about helping to fix a problem in code or to make a change to existing code to get the expected results. It is about implementing a new technology into the existing process. A forum is not the appropriate medium for someone to ask others to team them how to do something new. What usually works is for the person to do a little research (e.g. tutorials) and attempt to implement the new feature. Then, if problems are experienced or if advice is needed, then make a post as to what has been done and what is not working as expected. QuickOldCar pointed you to a google results page that you should have done yourself to at least do some research before asking a question. So, here is my advice. 1. If you have not used JQuery, you will want to for this. It makes AJAX (and many other things with JavaScript) much easier. There will be a learning curve, but for what you need to do, there are only a handful of things you will need to learn initially. 2. Fix your current code. FONT tags? Those have been deprecated for over a decade! Use SPAN tags with style properties. That code should be it's own separate file if it isn't already. The page which will display that content should use a DIV to contain the content <div id="last_post"><?php include('last_post.php'); ?></div> 3. Create a timed event using JQuery that will fire every n seconds. That event will use the the JQuery ajax load event such as $( "#last_post" ).load( "last_post.php" ); That will work, but it is inefficient. You should pass a timestamp to the ajax call with the last time a request was made. That way the query to get the last post can determine if there is new content or not. If not then no update needs to be sent back. http://api.jquery.com/load/ -
Did you try that query format that Barand provided? It should return the value in a formatted string: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
-
The DIV that tryingtolearn mentioned above IS in the code of your page. That is probably at least part of the problem. Just looking at the page and not the code, it appears to me that the content is too wide to fit, therefore it is being pushed below the right-hand section. You need to expand the width of the area where the content or reduce the container that holds that content. You can try widening these divs <div id="mainContent"> <div id="wpContent"> Check your stylesheet for references to those IDs. This appears to be the container for the section that is pushed to the bottom. <div id="maincontainer">
-
I don't know how it could have been explained any clearer.
-
Edit: Barand beat me to it, but I'm going to post anyway The error is pretty self explanatory. You are trying to execute a function called prepare on an object - but the object doesn't exist $stmt = $db->prepare(); $db is not an object - it is only a string $db = "a6382499_product"; Looks like $conn is your db object
-
ginerjm, I think you may have skimmed over some of the previous responses. As you know, if you reply to a topic, then you receive notifications when there are responses to that topic. But, there is also a button at the top of any topic called "Follow this topic". If you click that button, you would get notified to any changes to that topic - even if you did not post a reply in that thread. AND, at the top of each forum board there is a button called "Follow this forum". If you click that you would receive notifications about any updates on that entire forum. It is possible that you had intentionally or unintentionally clicked that option some time in the past. But, since email notification were turned off you would not have received email notifications at the time the option was clicked. When email notifications were re-enabled, they started coming through since that option had previously been selected. As mac_gyver stated He was kind enough to turn that option off for you.