如何在运行一次后使用jquery再次运行

时间:2015-07-17 02:15:59

标签: jquery wordpress

我计划构建一个wordpress插件。在一个页面中,我尝试使用jquery从某些类别的表中获取数据库。我想将这些数据放入表单并使用jquery修改结果。但我失去了一些帮助

的地方

这是我的代码

<?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";
            }

    }
?>

我的jquery代码是

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);
     }
     );
     });
    }

0 个答案:

没有答案