1

when ajax code is in running mode,at that time database entry stopped,when i comment ajax code that time database operation performed successfully, right now i am using ajax code and database insert code in same file.i know that it is better to use ajax code in another file,but i don't know how to separate ajax code in separate file and how to call that file in plugin main file.

<?php 
 function add_new_Student() {

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

        global $wpdb;
        $name = $_POST['name'];
        $role = $_POST['role'];
        $contact = $_POST['contact'];
//        var_dump($_POST);

        $table_name = $wpdb->prefix . 'Student_list';
        $wpdb->insert(
                "$table_name", //table
                array('name' => $name, 'role' => $role, 'contact' => $contact), //data
                array('%s', '%s', '%s') //data format
        );

        $message.="Student Added";
    }
    ?>
    <div class="container">    

        <form method="post" id="frmdemo" class="form-horizontal myform" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

            <div class="form-group">
                <label class="control-label col-sm-2" for="Name">Name:</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="name1" name="name" placeholder="Name">
                </div>
            </div>

             <div class="form-group">
                <label class="control-label col-sm-2" for="Role">Role:</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="role1" name="role" placeholder="Role">
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-sm-2" for="Contact">Contact:</label>
                <div class="col-sm-4">
                    <input type="number" class="form-control" id="contact1" name="contact" placeholder="Contact">
                </div>
            </div>

                    <button type="submit" class="btn btn-primary" name="insert"/>Submit</button>  
                    <button type="reset" class="btn btn-danger" name="reset"/>Reset</button>

                    <div id="error_message" class="ajax_response" style="float:left"></div>
                    <div id="success_message" class="ajax_response" style="float:left"></div>              

        </form>
    </div>

// ajax code here
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>         
<script>
$("#frmdemo").submit(function(e) {
    e.preventDefault();
    var name = $("#name1").val();
    var role = $("#role1").val();
    var contact = $("#contact1").val();


    if(name == "" || role == "" || contact == "") {
        $("#error_message").show().html("All Fields are Required");
    } else {
        $("#error_message").html("").hide();
        $.ajax({
            type: "POST",
            url: 'post-form.php',
            data: "name="+name+"&role="+role+"&contact="+contact,
            success: function(data){
                $('#success_message').fadeIn().html(data);
                setTimeout(function() {
                    $('#success_message').fadeOut("slow");
                    $('').fadeOut("slow");
                }, 2000 );

            }
        });
    }
})
</script>   


    <?php
}
?>

1 Answer 1

2

in ajax url: 'post-form.php', try to add url:'<?php echo get_permalink();?>', or

url: '<?php echo plugins_url("your_plugin_name/folderName/post-form.php"); ?>',
Sign up to request clarification or add additional context in comments.

13 Comments

Used this code but still ajax works fine,but databse entry can't performed. url: '<?php echo plugins_url("plugin_name/post-form.php"); ?>',
remove the function simple start from isset($_POST['insert']) coz you are not calling the function
actually this is plugins's file,the function is used at plugin's main file,so plugin is necessary to use,my question is i want to perform both action(ajax,database operation) at a same time
yes, ajax is working fine, when i comment ajax code then database operation works fine,but i want both operation at a same time.
check what data is sent by ajax
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.