Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. No, it would not. Despite the syntax error roopurt18 pointed out you can't do a redirect and then set a session variable. Once the redirect occurs, all subsequent code is not executed. My response was specifically about how you check if the value is set on subsequent pages using the session variable. If you have made a determination as to whether the checkbox was set or not, you should set the session variable to either true/false instead of relying upon isset() of the session variable. This is just good coding practice. This would make sense to anyone reading the code as to what the condition being checked is for if($_SESSION['ConsentCheckbox']===true) This could be easily misinterpreted if(isset($_SESSION['ConsentCheckbox'])) I already stated that the 2nd to last example you posted should work. You state this is a "theoretical exercise", but I would counter this is a useless exercise. Just because something can be done doesn't mean it should be done. I could probably reduce an entire page of code to a long combination of nested ternary operators. So what? What would be the benefit in doing so?
  2. Tables have a purpose - namely for displaying tabular data. But, it would be easy enough to modify that code to use on CSS and DIVs to get the same effect. Off the top of my head, I would set the default "float" style of each DIV as float:left. Then in the code above, whenever the last record in a row is encountered, set that value to nothing. That should cause a line break after each n record.
  3. The code you posted will not work. All PHP code is processed before the page is sent to the browser. Then once the page is received by the user's browser javascript code can be executed. So, you can't set a PHP variable through JavaScript to be used later in the same page.
  4. The process is pretty strait forward, but takes a little forethought and discipline. Just create your pages first without any javascript so you know they will work without JS being enabled. Then implement JS features so that they are replace normal HTML functionality. Then if the user does not have JS enabled you know that the existing code will work. If there are certain elements that you need displayd for the HTML only version, but not in the JS version, then just use an onload event to use JS to hode the element. Here is an example form with three different types of features that use JS and how they are handled for JS vs no JS. The first field allows the user to create their username. For a JS user we want to give them the option to check availability of the name before submitting the page. So, we will display a button for JS users that will do an AJAX call when clicked (I did not include the AJAX code). Also, there are two fields to enter a password and confirm it. So, we put an onsubmit call for the form. Of course this won't be run for a non JS user so we should always do validations server-side as well. Lastly, there is a radio button group where one value is "Other". For non JS users we will have a text field that is always enabled for them to enter the data if they select other. but, for JS users we want the field to be disabled unless they select other. <html> <head> <script type="text/javascript"> function checkUname() { //Run function to check if uname exists } function validateForm(formObj) { if(formObj.elements['pword'].value != formObj.elements['confirm'].value) { alert('Passwords do not match.'); return false; } //Form is valid return true; } function selectOther(otherSelected) { var otherFld = document.getElementById('hobby_other') otherFld.disabled = (!otherSelected); if(!otherSelected) { otherFld.value=''; } } window.onload=function() { //Display "Check Availability" button for JS users document.getElementById('checkBtn').style.display = ''; //Disable other text field for JS users document.getElementById('hobby_other').disabled = true; } </script> </head> <body> <h1>Create Account</h1> <form onsubmit="return validateForm(this);"> Username: <input type="text" name="uname" /> <button onclick="checkUname()" style="display:none;" id="checkBtn">Check Availability</button> <br /><br /> Password: <input type="text" name="pword" /> <br /> Confirm: <input type="text" name="confirm" /> <br /><br /> Favorite Hobby:<br> <input type="radio" name="hobby" onclick="selectOther(false);" value="Cars" /> Cars<br /> <input type="radio" name="hobby" onclick="selectOther(false);" value="Computers" /> Computers<br /> <input type="radio" name="hobby" onclick="selectOther(false);" value="Women" /> Women<br /> <input type="radio" name="hobby" onclick="selectOther(true);" value="Other" /> Other<br /> <input type="text" name="hobby_other" id="hobby_other" value="" /> <br /><br /> <button type="submit">Submit</button> </form> </body> </html> Some people even like to go so far as not including any inline JS - such as the onsubmit or onclick event calls in the elements. Instead they set those even properties in the onload event.
  5. And what error(s) are you getting? I think I know what the problem is, but not positive from your explanation. "frmName" & "ctrlName" are variable names created from values passed into the function. You cannot use them like this: if (document.frmName.ctrlName.UniqueID[i].checked) When that code is encountered the javascript parser is looking for a form with the literal name "formName". It does not evaluate the value of the variable "formName". But. the solution is simple. Just use the object collection reference and use the variable accordingly. Give this a try: if (document.forms[frmName].elements[ctrlName].UniqueID[i].checked) Although, I'm not sure that would work. What is UniqueID supposed to reference? It might help if you show the HTML form or at least the relevant part.
  6. Since it is only two digits grouped together I would just do the following for clarity /^\d\d-\d\d-\d\d$/
  7. Just to follow up, please heed the advice I gave previously. If I had provided a solution based upon how your first query was posted it would not have been what you needed. In the future provide a better description and/or details so the persons viewing your topic can understand better.
  8. Without knowing the functionality of your class "DateTime()" I'm not sure exactly where the change could be implemented. But, a simple solution based upon the dat ayou have would be to use strtotime(). http://us.php.net/manual/en/function.strtotime.php With that function you can convert a textual representation of a date/time into a timestamp. Plus, it allows you to do some ad-hoc addition/subtraction. So, if you ahve the current datetime in a string (not a timestamp) you can use the value of $user_time to create a timestamp offset per the user's setting.
  9. You can't resize it before uploading. Until the image has been uploaded the server doesn't have access to it. (OK, technically you could create create a Java application (not JavaScript) to resize on the clients computer before uploading, but that opens up bigger issues). You will want to upload the image and then resize it. There are a ton of resizing scripts available.
  10. Why would you give all the answers inputs the same name - across all questions? You should give them the same name for the same question. You don't give them the same name for ALL questions. So, for example, you give all the asnwers for question 1 a name such as "answer[1]", the answers for question 2 would be "answer[2]", etc. Then, the user will be able to select an answer for each question. On the receiving page you can access the answers using the appropriate names above. Example, based on the page you linked to: Question number 1 : adfasdf<br /> <input type="radio" name="answer[1]" value="good"/>adfasdf<br /> <input type="radio" name="answer[1]" value="bad"/>asdfasd<br /> <input type="radio" name="answer[1]" value="nogood"/>fasdf<br /> Question number 2 : sdafasd<br /> <input type="radio" name="answer[2]" value="good"/>fasd<br /> <input type="radio" name="answer[2]" value="bad"/>asdfasd<br /> <input type="radio" name="answer[2]" value="nogood"/>adfasdf<br /> Question number 3 : sadf<br /> <input type="radio" name="answer[3]" value="good"/>adfasdf<br /> <input type="radio" name="answer[3]" value="bad"/>adfasdfasdf<br /> <input type="radio" name="answer[3]" value="nogood"/>sadfasfd<br />
  11. Ah, it looks like I added the error in the last edit I did. Ending double quote was in wrong place. Use this: $query = "INSERT INTO cars (`CarName`, `CarTitle`, `CarPrice`, `CarMiles`, `CarDescription`) VALUES('{$CarName}','{$CarTitle}','{$CarPrice}','{$CarMiles}','{$CarDescription}')";
  12. I didn't say to hide the field, I said to make it a hidden field. There is a difference. The select field is created within a variable and you didn't show how that variable is created. But, here is the basic format of a hidden field <input type="hidden" name="foo" value="bar" /> Alternatively, you could keep it as a select field and hide it using CSS styles. <span style="display:none;">-- select field goes here --</span>
  13. As I stated, the string variable $query is malformed. $query = "INSERT INTO cars VALUES" ('','$CarName','$CarTitle','$CarPrice','$CarMiles','$CarDescription'); The value of $query is "INSERT INTO cars VALUES" because you end the query with the double quote at the end of that line. The second line with the values is not in the string and is what is causing the error. I provided a properly created sting in my sample code above. But, now you are stating that the table only has five fields. I assumed tha the first field was an ID field because in the values you have 6 different values, the first being an empty string. Personally, I prefer to explicitly state the fields to insert the values into. $query = "INSERT INTO cars (`CarName`, `CarTitle`, `CarPrice`, `CarMiles`, `CarDescription`) VALUES"('{$CarName}','{$CarTitle}','{$CarPrice}','{$CarMiles}','{$CarDescription}'); EDIT: Regarding the erros you are getting, most likely your database connection is not being made. Change the connection lines to the following: mysql_connect(localhost,$username,$password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error());
  14. <?php $username="wormste1_barry"; $password="barry"; $database="wormste1_barry"; $CarName = mysql_real_escape_string(trim($_POST['CarName'])); $CarTitle = mysql_real_escape_string(trim($_POST['CarTitle'])); $CarPrice = mysql_real_escape_string(trim($_POST['CarPrice'])); $CarMiles = mysql_real_escape_string(trim($_POST['CarMiles'])); $CarDescription = mysql_real_escape_string(trim($_POST['CarDescription'])); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO tablename VALUES ('','{$CarName}','{$CarTitle}','{$CarPrice}','{$CarMiles}','{$CarDescription}')"; mysql_query($query); mysql_close(); ?> Note: moved the variable creation for the field values to after the database connection since mysql_real_escape_string() requires a db connection and it makes no sense defining the variables if the db connection would fail.
  15. The query string is malformed. The values aren't in the string. Your script is also open to sql injection attacks.
  16. Probably, LOL. The second to last code block above is probably the best way to achieve what you want using the ternary operator. But, here is my two cents... I love using the ternary operator - especially for setting a variable to one of two values based on a condition. But, I only use it when it will help simplify my code. In the above, I feel, that by trying to condense the code down you are reducing the readability of the code. That will make the code more difficult to read/review/debug/etc. Then why set the value to the string value of 'true'. Personally, I would set the value to the boolean true and do a test such as if($_SESSION['ConsentCheckbox']===true) That makes more sense to someone reading the code than just using isset() since that person might not know that just being set is the determination as to whether the user actually accepted. To put it another way, I am all for simplification but not at the expense of clarity.
  17. Just change it to a hidden field with the same name with whatever default value you want to use.
  18. I would suggest using a table - displaying tabular data is what they are for. Here is one possible solution: <?php //Set maximum columns for the output $max_columns = 3; include_once("include/globals.php"); //Get database Results $query = "SELECT * FROM Products WHERE is_active = 1"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)===0) { $output = "No records found.\n"; } else { $output = "<table>\n"; //keeps getting the next row until no more records $recNo = 0; while($row = mysql_fetch_array($result)) { $recNo++; //Start new row when needed if($recNo%$max_columns==1) { $output .= "<tr>\n"; } //Create TD for record $output .= "<td>"; $output .= "<div class=\"title\">{$row['Name']}</div>"; $output .= "<div class=\"image\">"; $output .= "<a href=\"product_detail.php?id=\"><img src=\"{$row['image']}\" width=\"100\" alt=\"\" border=\"0\"></a>"; $output .= "</div>"; $output .= "<div class=\"tag_line\">{$row['Tag_Line']}</div>"; $output .= "<div class=\"price\">Now Only: £{$row['Website_Price']}</div>"; $output .= "<div class=\"prod-footer\"><a href=\"product_detail.php?id={$row['ID']}\">more info</a></div>"; $output .= "</td>\n"; //Close row when needed if($recNo%$max_columns==0) { $output .= "</tr>\n"; } } //Close final row if needed if($recNo%$max_columns!=0) { $output .= "</tr>\n"; } $output .= "<table>\n"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link href="styles/stylesheet.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="main"> <?php echo $output; ?> </div> </body> </html>
  19. That makes no sense. As prasanthmj stated, why not have one form with the various radio buttons. You can present the radio buttns in such a way as they look like five different forms. If you were to explain exactly what you were trying to achieve it would be easier to provide a solution. The only thing that would possibly make sense for what you are asking is if the five forms are supposed to subbmit to different domains. If that is not the case then you do not need/want separate forms. If you are trying to simultaneously submit data to multiple domains then you have at least two options that I can think of. 1. Use AJAX to submit the data from the form data to the appropriate domains (this, however, doesn't require multiple forms since the logic would be handled in javascript) 2. Create one form as already proposed and create a single PHP processing page that will then repost the appropriate data to the appropriate domains.
  20. Or maybe you can post a question with enough information for us to actually help you. I have no idea what you mean by 5 forms. Do you mean five different forms on the same page or 5 forms that the user would enter data into one by one. We are not mind readers here, please give some details instead of a generalized statement and we will be happy to help.
  21. I'm really not following your explanation well, but I think I know what you are wanting. if (exclude.x.indexOf(x) !== -1 && exclude.y.indexOf(y)===exclude.x.indexOf(x)){ That will chek if the X value is in the excluded list. If so, it then checks if the index of the Y value is the same as the index of the X value. So, the //dismiss branch will only be run if the X & Y values are corresponding values in the two arrays.
  22. Yes, absolutely you can. But, by using the same name for foreign keys does simplfy things and makes debugging much easier. BUt if the field names were different, this is one way to join the tables SELECT * FROM tbl_items JOIN tbl_items_categories ON tbl_items.category_id = tbl_items_categories.id By the way, I used '*' in my examples above, but I am of the opinioin you should always explicitly include the field names in the SELECT statemetn for the data you want.Using '*' increawses overhead on the server and can lead to potential problems - especially when joining tables
  23. Yes, you want to use a JOIN. Also, they are not "rows", they are "fields" or "columns". Since both tables use the same field you doa JOIN using the USING command. SELECT * FROM tbl_items JOIN tbl_items_categories USING `category_id`
  24. Just to add: it is *possible* that the site is coded to accept login variables on the URL. There are some that do that with the username and some with the password as well. However, you would have to know if they are allowed and what the parameter names are. Here is what you could try. View the source of the login page and see what the field names are for the username and the password. Then add those namess along with their respective values to the url and see if that gives you access to the page without being ogged in already.
  25. This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=309804.0
×
×
  • 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.