0

At the moment I save data when changes are made on the page using ordinary PHP but of course it makes the page reload every time which resets everything. I really need to do it without page reload. I've heard its possible to do with Ajax and JQuery. I would much prefer Jquery answers rather than Javascript.

4
  • 1
    have you tried something ??if yes then show it . if no then try first . Commented Jul 9, 2015 at 6:55
  • Jquery IS Javascript, Feel free to research some basic elements of your question, find some code samples, try them and then if you have problems. Post them specifically here. There is also no actual question in your post. Commented Jul 9, 2015 at 6:55
  • w3schools.com/jquery/ajax_ajax.asp read this you can post data to save. Commented Jul 9, 2015 at 6:56
  • No I haven't tried anything otherwise I would have posted it. I only have it working in php with a page reload right now. Commented Jul 9, 2015 at 8:04

2 Answers 2

3

for example you have this form

<input type="text" name="name" class="abc" id="test" placeholder="Name">
<a href="#" class="PopUpSendBtn" id="sendinfo">Send</a>

then write Script

<script>
    $(document).ready(function() {
   // invoke event on clicking send button
   $("#sendinfo").on("click", function(evt) {
   // get value of input field
var userName = $('#test').val();

                         jQuery.ajax({
                url: filename.php,
                type:'POST',
                async: false,
                data:{'userName':userName},
                success: function(output_string){
                    //    alert(output_string);
                    if(output_string == 1){
                        // do someting
                    } else { 
                        // do someting
                    }
                   }
                   , error: function(object, status, response) {
                    alert(response); 
                }
                });

    });
});
</script>

your php file(filename.php) get data using post request

 $data = $_POST['userName'];
// query to store data in db
// after saving data successfully in db your can send back a success message //in json or any other format (in my case i am using json)
         $data['success'] = TRUE;
        echo json_encode($data);

Don't forget to include jquery script.

This is the basic idea how to send data using ajax. You can modify it according to your requirements.

Sign up to request clarification or add additional context in comments.

Comments

-1

For me worked better to save all my data in the SESSION so to retrieve them every time i need. With AJAX works really fast and good. In this way all my data are saved also if the page refreshes. Obviously you have to declare all the variables you intend to use at the first step or it will never work!

Comments

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.