Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Thanked 20 Times in 20 Posts

    Is there a better way?

    I'm currently expanding on an online trivia game that I have written where people can add questions and edit the questions. The game works pretty good and I have the add/edit section working, but I was wondering if there was a better way to grab the date from the php file called forward.php? It seems there should be a better way to pass variables to the forward.php file ( url: "forward.php?direction=" + id + "&lastRec=" + lastId, // The PHP file that retrieves the questions: )? It's not a big deal for this portion of the script works.
    	function forwardFCN() {
    		$.ajax({ // Start of ajax:
    			url: "forward.php?direction=" + id + "&lastRec=" + lastId, // The PHP file that retrieves the questions:
    			dataType: "json", // Format type:		
    			success: function(info) { // Grab the data from php and then display it:
    				id          = info.trivia_id;
    				lastRec     = info.lastRec;    
    				question    = info.trivia_question,
    				answer1     = info.answerA,
    				answer2     = info.answerB,
    				answer3     = info.answerC,
    				answer4     = info.answerD,
    				correct     = info.correct;
    				$('#editId').attr('value', id);
    				if ( info.confirmed == 'yes' ) {
    					$('input:radio[name=confirm]')[0].checked = true;
    				} else {
    					$('input:radio[name=confirm]')[1].checked = true;
    				$answerA.attr('value', answer1);
    				$answerB.attr('value', answer2);
    				$answerC.attr('value', answer3);
    				$answerD.attr('value', answer4);
    				$('option.editOption').each(function() {
    					if ( $(this).attr('value') == correct ) {
    						$(this).attr('selected', 'selected');
    			} // End of fetching data:
    		}); // End of Ajax:
    	} // End of Call Ajax Function:
    Here's the php file that goes with the above Ajax call:
    PHP Code:
    if (isset(
    $_GET['direction']) || !filter_var($_GET['direction'], FILTER_VALIDATE_INT, array('min_range' => 1))) {

    $edit_id $_GET['direction'];
    $lastRec $_GET['lastRec'];    
    $e = array();

      if (isset(
    $current_id)) {

        try {
    $query "SELECT  id, confirm, question, answerA, answerB, answerC, answerD, correct FROM movieTrivia WHERE id>=:current_id ORDER BY id ASC LIMIT 1";
    $stmt $pdo->prepare($query);
    $result $stmt->execute(array(':current_id' => $edit_id));

    // If the query ran OK, fetch the record into an object:
    if ($result) {

    $trivia $stmt->fetch();
    $e['trivia_id'] = $trivia->getId(); 
    $e['confirmed'] = $trivia->getConfirm();
    $e['trivia_question'] = $trivia->getQuestion();
    $e['answerA'] = $trivia->getAnswerA();
    $e['answerB'] = $trivia->getAnswerB();
    $e['answerC'] = $trivia->getAnswerC();
    $e['answerD'] = $trivia->getAnswerD();
    $e['correct'] = $trivia->getCorrect();

          } else {
            throw new 
    Exception('An invalid page ID was provided to this page');
        } catch (
    Exception $e) { // Catch generic exceptions

    Any help or insight will be greatly appreciated.

    Best Regards,

    P.S. If anyone is curious what the game looks like, here's a link to it:https://www.pepster.com/ and it's even responsive.
    Last edited by Strider64; 06-09-2014 at 02:29 PM.

  2. #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 441 Times in 437 Posts
    1) even for jQuery AJAX GET requests you can put your data into the data config property. you could also choose to use the $.getJSON() shorthand method.

    2) [PHP]
    - there is a filter function to use input directly:
    PHP Code:
    // does not need a previous check for isset($_GET['direction'])
    // if this entry does not exist, filter_input() will return NULL
    filter_input(INPUT_GET"direction"FILTER_VALIDATE_INT, array('min_range' => 1))); 
    - $_GET['lastRec'] is not used => remove it.

    - $edit_id = $_GET['direction']; only hides that you’re using userland data. you can feed the GET data directly to PDO (and you are always reminded that this is unsafe user data)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. Users who have thanked Dormilich for this post:

    Strider64 (06-10-2014)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts