1

I am planing to build a wordpress plugin. In one page I tried to fetch the database from table of some categories using jquery. I want to put this data into forms and modify results using jquery. But I am lost some where pls help

here is my code

<?php
    function quizzy_edit_questions_page(){
?>
        <form id="quizzy-s-form" method="POST" action="" >
            <select name="Subject">
                <?php
                    global $wpdb;  
                    $ques = $wpdb->get_results("SELECT DISTINCT Subject FROM wp_quizzy;");

                    foreach ($ques as $que){
                        echo "<option value=".$que->Subject.">".$que->Subject."</option>";
                    }
                ?>
            </select>
            <input type="submit" name="submit" id="submit" class="button button-primary" value="Select">
        </form>

        <p id="result"></p>


        <?php
            // code for fetching questions

            if(isset($_POST['Subject'])){

                global $wpdb;
                global $sub;
                $sub = $_POST['Subject'];  // Storing Selected Value In Variable
                echo "<p>You have selected :" .$sub."</p>";  // Displaying Selected Value

                global $wpdb;  

                require_once( dirname(__FILE__) . '../../../../wp-load.php');

               $ques = $wpdb->get_results("SELECT * FROM wp_quizzy WHERE Subject = '$sub';");
               $q_num = count($ques)   
        ?>

        <html><h2>Number of Questions in <?php echo $sub; ?> are <?php echo $q_num; ?>.</h2>
        <?php
             global $wpdb;  
             $ques = $wpdb->get_results("SELECT * FROM wp_quizzy WHERE Subject = '$sub';");

             foreach ($ques as $que){
                 echo "<p><b>Q.)".$que->Question."</b></p>";
                 echo "<p><b>A.)".$que->Choice1."</b></p>";
                 echo "<p><b>B.)".$que->Choice2."</b></p>";
                 echo "<p><b>C.)".$que->Choice3."</b></p>";
                 echo "<p><b>D.)".$que->Choice4."</b></p>";
                 echo "<p><b>Answer:".$que->Answer."</b></p>";
                 echo"<br>";    
             }

        foreach ($ques as $que){
        ?>
             <form id="quizzy-sub-form" method ="post" action ="<?php echo plugins_url( 'EditQuestion.php', __FILE__ );?>" >
             <?php
                 echo"<b>Subject:</b><input type='text' name = 'Subject' size = '50' value ='".$que->Subject."'><br>";
                 echo"<b>Question:</b><input type='text' name = 'Question' size = '50' value ='".$que->Question."'><br>";
                 echo"<b>Choice1:</b><input type='text' name = 'Choice1' size = '50' value ='".$que->Choice1."'><br>";
                 echo"<b>Choice2:</b><input type='text' name = 'Choice2' size = '50' value ='".$que->Choice2."'><br>";
                 echo"<b>Choice3:</b><input type='text' name = 'Choice3' size = '50' value ='".$que->Choice3."'><br>";
                 echo"<b>Choice4:</b><input type='text' name = 'Choice4' size = '50' value ='".$que->Choice4."'><br>";
                 echo"<b>Answer:</b><input type='text' name = 'Answer' size = '50' value ='".$que->Answer."'><br>";
                 echo"<input type='hidden' name = 'id'value ='".$que->id."'><br>";
                 echo"<input type='submit' id= 'submit' value='submit' name='submit'>";
                 echo"</form><br><br>";

                 echo"<p id='result'></p>";

             }

            }else{

            echo"Select the Subject";
            }

    }
?>

and my jquery code is

jQuery(document).ready(function($){
    quizzy-sub();   

});

function quizzy-sub(){

    $('#quizzy-sub-form').submit(function(){
     event.preventDefault();
    });

    $('#submit').click(function(){
     $.post( 
     $('#quizzy-sub-form').attr('action'),
     $('#quizzy-sub-form :input').serializeArray(),
     function(result){
     $('#result').html(result);
     }
     );
     });
    }
Suri
  • 43
  • 8
  • Question doesn't make sense....jQuery can't get anything from a database and the code you've shown is all php, no jQuery or javascript. Need a far better explanation of what you are trying to do and where the problems are – charlietfl Jul 17 '15 at 02:51
  • I added the jQuery code also now.In question I mean loading results from a page – Suri Jul 17 '15 at 03:28
  • Still not clear what your problem is. Also can't use hyphen in function name – charlietfl Jul 17 '15 at 03:29
  • I will explain.initially from a form i fetched questions from a selected subject.Now I propagated the results into another form. Now I want to modify these data and update it when the user selects submit. But here my question is when the user submits the page is redirecting to another page which i dont want and to use jquery or some other script to do that ...I think I am clear now @charlietfl – Suri Jul 17 '15 at 03:37
  • Unrelated to your problem, but _please_ don't assume `$_POST['Subject']` will contain an expected value. Your code is a classic example of a SQL injection vulnerability. At the very least, use [`$wpdb->prepare`](https://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks). How may also want to look at [`htmlspecialchars`](http://stackoverflow.com/questions/46483/htmlentities-vs-htmlspecialchars) when you're outputting `$sub` in your HTML (eg for this line `echo "

    You have selected :" .$sub."

    ";`)
    – Hobo Jul 17 '15 at 07:37
  • Use event delegation for submit button click since you are replacing that button. Events are lost when replacing elements – charlietfl Jul 17 '15 at 13:06
  • I came to know that the elements which are not showing in page source i.e exact html which is not coming from server cant be manipulated with jQuery. – Suri Jul 18 '15 at 15:53

0 Answers0