Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,356
  • Joined

  • Days Won

    173

Everything posted by mac_gyver

  1. not really. you are doing the same operation, only the name/meaning of the data is different. you should have a booking/reservation/order table, that holds the unique/one-time booking data. a single row is inserted into this table when someone submits an order. this row produces a booking id (the autoincrement primary index.) you would use this booking id to store the related booking item data, one row per item, which in this case is the seat id, screening id, ...
  2. to get php to cause a http 500 status for fatal syntax/runtime errors, php's display_errors setting needs to be set to OFF. you would then want the log_errors setting to be set to ON, so that you have a record of what errors are occurring. also, php's error_reporting needs to always be set to E_ALL or a -1.
  3. what is your overall goal here? OOP is not about wrapping your main code in class(es), adding $var-> in front of everything, and making a wall of code that takes 10x the number of lines of code to accomplish a task.
  4. are you doing this as a learning exercise? what is your goal? some pointers - the code for any page should be laid out in this general order - 1) initialization, 2) post method form processing, 3) get method business logic - get/produce data needed to display the page, 4) html document. the post method form processing should not attempt to detect if the submit button is set, there are cases where it won't be. instead, detect if a post method form was submitted before referencing any of the form data. keep all the form data as a set, in a php array variable, then operate on elements in this array variable throughout the rest of the code. trim all the input data before validating it, mainly so that you can detect if all white-space characters were entered. validate all the trimmed input data at once, storing user/validation errors in an array, using the field name as the main array index. after the end of the validation logic, if there are no errors (the array holding the user/validation errors is empty), use the input data. since all you are doing is comparing an input value with the correct answer, you can do this as part of the validation logic. if you were storing data in a database, authenticating a user, sending an email, ... you would put the code needed to perform these actions here. after using the input data, if there are no errors, perform a redirect to the exact same URL of the current page to cause a get request for the page. this will prevent the browser from trying to resubmit the form data should the page get reloaded or browsed back to. to display a one-time success message, store it in a session variable, then test, display, and clear that session variable at the appropriate location in the html document. if there are user/validation errors, the code will continue on to display the html document, display any errors, redisplay the form, populating the field values with any existing data, so that the user doesn't need to keep reentering values over and over. any dynamic value you output in a html context needs to have htmlentities() applied to it to help prevent cross site scripting. there's a programming issue with the division operator and computers. this operation can result in a fractional part that cannot be represented exactly in a computer and then cannot be easily compared. you may want to test the answer produced is this case and limit the question/answer to those which only have whole integer answers, i.e. keep something like 9/3, but not 7/6, and also don't allow division by 0. if you do all of that, except for handling the division cases, you would end up with code that looks like this - <?php // initialization // the error related settings should be in the php.ini on your system error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); $post = []; // array to hold a trimmed working copy of the form data $errors = []; // array to hold user/validation errors // post method form processing if($_SERVER['REQUEST_METHOD'] === 'POST') { // inputs: number_entered, $_SESSION['answer'] // trim all the post data at once $post = array_map('trim',$_POST); // if any input is a array, use a recursive trim call-back function here instead of php's trim // validate inputs if($post['number_entered'] === '') { $errors['number_entered'] = 'You must enter a number'; } // note: this assumes that only integer answers are permitted (in the case of the division operator) else if((int)$post['number_entered'] !== (int)$_SESSION['answer']) { $errors['number_entered'] = "Incorrect guess<br>The correct<br>number was <b>{$_SESSION['answer']}</b> <br><img src='sorry-tryagain.png'><br>"; // since you are displaying the correct answer, you would want to generated a new question in this case? //unset($_SESSION['question']); } // if no errors, success if(empty($errors)) { $_SESSION['success_message'] = "<img src='you-win.png'><br><b>{$_SESSION['answer']}</b> IS THE<br>CORRECT GUESS!</b><br>"; // to continue, you would generated a new question //unset($_SESSION['question']); // redirect to the exact same url of the current page to cause a get request - PRG Post, Redirect, Get. die(header("Refresh:0")); } } // get method business logic - get/produce data needed to display the page // if there's no question/answer, generate one if(!isset($_SESSION['question'])) { $rand1 = rand(0, 9); $rand2 = rand(0, 9); $operator = array('*', '/', '+', '-'); $randoperator = $operator[rand(0, 3)]; switch ($randoperator) { case "+": $finaalvalue = $rand1 + $rand2; break; case "-": $finaalvalue = $rand1 - $rand2; break; case "*": $finaalvalue = $rand1 * $rand2; break; case "/": // note: this can produce a fractional number, which you must take care with when performing comparisons. // also division by zero. $finaalvalue = $rand1 / $rand2; break; } $_SESSION['question'] = "$rand1 $randoperator $rand2 = "; $_SESSION['answer'] = $finaalvalue; } // html document - this is an incomplete document. it only shows the necessary parts for the demonstration. ?> <?php // display any success message if(isset($_SESSION['success_message'])) { echo $_SESSION['success_message']; unset($_SESSION['success_message']); } ?> <?php // display any errors if(!empty($errors)) { echo "<p>".implode('<br>',$errors)."</p>"; } ?> <?php // display the form if(!empty($_SESSION['question'])) { ?> <form method="POST"> <br><b>Level 1<br>Do The Math</b><br><br> <?=$_SESSION['question']?> <input type="text" name="number_entered" value="<?=htmlentities($post['number_entered']??'',ENT_QUOTES)?>" autocomplete="off"><br><br> <input class="button" type="submit" value="Enter Guess"><br><br> </form> <?php }
  5. web servers are stateless. they don't know or care what has happened outside of the current request. each time your code runs, it generates new random values. if you want to remember these values, from one request to the next, you need to store them in session variables, and only generate new ones if the session variables are empty/not-set.
  6. lol. all the javascript posted for this problem is unnecessary. upon the DOM being loaded/rendered, it's getting data that's known at the time of the request for the map2.php page (the value being sent to getBookedSeats.php is coming from a js variable that's being set to a php value echoed on the page.) this is just a roundabout wall of code and data churn. here's a list of why this is not working - 1. you are making a POST request to getBookedSeats.php. the value won't be in any $_GET variable and adding an isset() won't make it work. all that did is hide the problem and caused the php code to be skipped over. 2. you are sending JSON encoded data to getBookedSeats.php. you would need to use the following to read and decode the data - $json = file_get_contents('php://input'); $data = json_decode($json,true); 3. the value will then be in $data['screeningId'], because that's the name of the javascript variable holding the value that you are sending in the ajax fetch request, which is a value that is coming from php in the first place.
  7. this code is apparently sending either a text or email with a one-time-pin. my guess is it isn't displaying the pin entry page, eventually times out, and redirects to the dashboard page. you would need to show or state what exactly does happen and what you expect to happen. the only things I can tell you based on the posted code are - don't use the @ error suppressor. If you want to test if a variable is set, either use isset()/!isset() or use the Null coalescing operator to condition the input to a default false value. the first header() redirect needs an exit/die statement to stop php code execution, like the rest of the code is using. whatever your inputValidation() function does, it probably doesn't make a value safe to put directly into an sql query. correctly use a prepared query, like the rest of the code is using.
  8. you probably have a redirect-loop or code that's caught in a loop in php. you would need to post all the code, less any database connection credentials, for the login operation and at least the login check code from one of the other pages. btw - the only redirect you should have in your login code should be to the exact same URL of the login page to cause a get request for that page. this will prevent the browser from trying to resubmit the form data should that page get reloaded or browsed back to, where someone can use the browser's developer tools to see what the form data is, even if you prevent the form from being displayed. to allow someone to go to another page, provide navigation links, or put the login form processing/form on any page that needs it.
  9. is this line in getBookedSeats.php or map2.php? at the point where you posted it, the subject was "things being output by getBookedSeats.php"?
  10. just to summarize for anyone reading this. the map2.php code is making ajax fetch requests to get data. the error that is occurring is for a request to - getBookedSeats.php. It is the getBookedSeats.php code that must only output json encoded data. what is the full code for getBookedSeats.php?
  11. if you open the network tab, first, then trigger the request in question, there will be an entry showing the name of the file that got requested. if you click on the file name, you will get a set of tabs, one of which is the Response.
  12. look at the actual response in the browser's developer tools, network tab. the page you make the ajax request to, must only return the json encoded data. there can be nothing else output with that request. if you are not already doing so, the code for any page should be laid out in this general order - initialization post method form processing get method business logic - get/produce data needed to display the page html document for a page that produces a json response, you would output the response at the end of item #3 on this list, then there would either be no actual html document section or you you would exit/die to stop the php code execution on the page. if none of the replies help solve the problem, just post all the code needed to reproduce the problem, since you have a misunderstanding of what the code should be doing. snippets of the problem don't tell us things like -
  13. if you put the form fields inside a form, and give them array names, you can use the jquery .serialize() method to get the form data using a single line of code - https://api.jquery.com/serialize/ you would use this in the data value in the .ajax call, e.g. data: $(this).serialize(), next, if you put the dynamically to-be added markup inside a <template></template> tag, it is simple to dynamically add it - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template if you put the closing </label> tag after the form field it corresponds to, you can leave out the for='...' attributes and the corresponding id='...' attributes, and make the labels work for the dynamically added fields.
  14. each page must enforce what the current user can do and see on that page, for a non-logged in user, for a logged in user, and for a logged in administrator. if the current user is not logged in, they can only do and see what you have coded on that page for a non-logged in user to do and see. if they are a logged in user and the user ID in the URL is the same as the currently logged in user ID, they can perform actions like editing their own data and seeing all of their own data. if the currently logged in user is a administrator, he/she would be able to pick any user and be able to perform actions like editing that user's normal data and seeing all that user's normal data, and be able to edit/see additional data, such as permissions, edit history, site activity, ip history, ... if you aren't already doing so, your login code should store the user id (autoincrement primary index) in a session variable to indicate who the currently logged in user is. if there is a logged in user, you would query on each page request to get any other user data, such as - username, email, permissions, ...
  15. or you could read the detailed reply you got in your 'proper error handling' thread - https://forums.phpfreaks.com/topic/318166-proper-error-handling/?do=findComment&comment=1615758
  16. you would need to use JSON.stringify({page:2}) for there to be a 'page' element in the decoded data.
  17. here's something that will save you a ton of duplicate effort. client-side validation is a nicety for legitimate visitors. data sent to your web sever can come from anywhere, not just your forms/links/cookies, can be set to anything, despite any client-side validation you may have, and cannot be trusted. you must trim, mainly so that you can detect if a value is all white-space characters, then validate the trimmed data, on the server, before using it. since you must do this on the server, you should either just use the browser's built-in form validation or use ajax to send the piece(s) of data to the server for pre-submission validation, then validate it again, on the server, when the form has been submitted.
  18. most database errors are either due to programming mistakes or a database server that's not running. these type of errors are not recoverable by the user, and the user or hacker on a site doesn't need to know anything specific when these type of errors occur. however, you, as the programmer/developer, do want to know when these type of errors occur. therefore, when learning, developing, and debugging code/query(ies) you would like to display the raw database statement errors, so that you get immediate feedback about problems. when running your application on a live/public server, you would like to log the raw database statement errors, so that you have a record of them, and can find and fix what's causing them. the PDO extension has always used exceptions for connection errors. you should use exceptions for all the other database statements that can fail - query, exec, prepare, and execute. in php8+, the default setting now is to use exceptions for all the database statements that can fail (for both the PDO and mysqli extensions.) you should only catch and handle database exceptions in your code for user recoverable errors, such as when inserting/updating duplicate or out of range user submitted data. the exception catch logic would test the query error number, and setup a message for the user letting them know what was wrong with the data that they submitted, so that they can potentially correct what is wrong, and resubmit the data. for all other query error numbers, just rethrow the exception and let php handle it. for all other types of queries, simply do nothing in your code and let php catch and handle any database exception. when php handles an exception, php will use its error related settings to control what happens with the actual error information, via an uncaught exception error (uncaught exceptions will 'automatically' get displayed/logged the same as php errors.)
  19. the double posted code is the same form, but with a different amount removed for posting by the op. the 13 lines starting with the id="package-step07" div has been additionally removed from the second posting. code doesn't just stop working, so you will need to investigate what is actually occurring. is the form processing code actually getting executed? are there any php errors getting displayed/logged? you will likely need to check with your web server administration as to what is happening with these emails on the mail server. a bunch of recommendations - email can be unreliable. you should store the submitted form data in an database table or a log file so that you have record of the form submissions. the form processing code should NOT copy variables to other variables for nothing. this is just a waste of typing. keep the form data as a set in a php array variable, then operate on elements in this array variable throughout the rest of the code. as a more advanced programming task, if you have more than 2-3 form fields, you should use a data-driven design and dynamically validate and process the form data. you need to trim all the input data before using it, mainly to detect if all white-space characters were entered. after you do item #2 on this list, you can accomplish this will one single line of code. you need to validate all the trimmed input data before using it, storing user/validation errors in an array using the field name as the main array index. 'required' fields must not be equal to an empty string and fields that must have a specific format, such as an email address, must be validated to insure they have that format. after the end of the validation logic, if there are no validation errors (the array holding the user/validation errors will be empty), use the form data. you need to apply htmlentities() to all the submitted form data, after validating it and before using it in an email or in form field values, to help prevent cross site scripting. you must test the return value from the mail() call. if it is a false value, setup a general failure message for the user and log all the relevant data from the request, such as - date/time, ip address, form data, ... the form fields should be 'sticky' and repopulate the field values upon any user/validation errors, so that the user doesn't need to keep reentering data over and over. note: if there are user/validation errors, you would redisplay the form, therefore it should not be inside the else conditional for a post method form having been submitted. if this stopped working, the most likely cause is that it is being used to send spam. by not validating the submitted email address (see item #5 on the above list) and putting the submitted value into the mail header, a spammer can send anything to anyone he wants through your mail server.
  20. nothing i wrote concerns a fixed amount for each fee type. the fee type is the name/meaning of the fee and by submitting the correspond fee_id and storing that, you are normalizing the data, which will result in the least amount of data storage and the fastest queries. the amount field should actually get populated with the current default amount for the selected fee type (i would use data- attributes and some javascript), for those cases where the standard fee will be used.
  21. this is at least the 3rd time someone has stated in your threads to not store derived values. you just calculate them when needed. and if you store the raw data properly, it is a simple and fast query to do so. you should (and probably do) have a student table, which holds the unique/one-time student data. this defines the student_ids. any data related to a student should use the student_id. you should not have any other values from the student table, such as the student_name, in any other table. you should (and probably do) have a semester table, which holds the unique/one-time semester data. this defines the semester_ids. you should (and probably do) have a class table, which holds the unique/one-time class data. this defines the class_ids. you should have a register table, which i suspect is what the invoices table is trying to be, that holds a row for each class, for each semester, for each student. this table defines the class_semester_student_ids that should be used when storing related data, such as invoices, assignments, exams, ... to keep track of the invoice data, per my reply in your previous most recent thread - "you should have a 'fee' table that holds the different type of fees/discounts. you would then have a select/option menu to select the fee type, which submits the fee_id, and the fee amount." you would insert a row for each fee/discount for each class_semester_student_id into an invoice_item(s) table (currently named items_table.) to get the current amount for any or all fee type(s) for any or all class(es), semester(s), or student(s), you would simply SUM() the +/- amounts for the records matching the WHERE term that you build.
  22. where exactly are you getting/seeing the php warnings? in the browser's developer tools network tab, what are 'all' the requests, not just the Fetch/XHR?
  23. your full code is probably doing something like making two requests, one with out data and one with. you would need to post all your code to get specific help with what is wrong with it.
  24. in the network tab, if you click on the URL, which is apparently save_invoice.php, it will show you a list of tabs for the request and response data.
×
×
  • 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.