shubham kapoor
shubham kapoor

Reputation: 599

Insert the data in MySQL without reloading the page and get the data at same page

I am using ajax but it behaves different. I just want code that I should in Model , VIEW,Controller.

This while contain two operation insertion and fetching the data from that db.

![In image their is subject and textarea as input.When i click submit the input dispaly in comment which is just above the Subject input]1

View

   <form action="" method="POST">
   <input type="hidden" name="myId" id="myId" value="" />
   <div class="form-group px-4">
       <label for="subject">Subject</label>
       <input type="text" id="js_subject" name="js_subject">
   </div>
   <div class="form-group px-4">
      <label for="exampleFormControlTextarea1">Example textarea</label>
      <textarea class="form-control" name = "js_text" id="js_text" rows="3"></textarea>
   </div>
  <input type="submit" id="comment-info" value="submit" name="submit" class="btn btn-primary">
   </form>

Upvotes: 0

Views: 22

Answers (1)

Laki96
Laki96

Reputation: 21

Use an jQuery Ajax request,and on somepage.php use if and else for insert and select and echo some message for both and die();

$('form').on('submit', function(event){
                event.preventDefault();
                $.ajax({
                        type:'post',
                        url:'somepage.php',
                        data:$(this).serialize(),
                        success:function(data){
                        var tmp = data;
                       if(data = "message1"){
                          //do whatever
                       }else if(data = "message2"){
                         //do whatever


                        }
                    })

                    });

Upvotes: 2

Related Questions