1

I just need to display names of the array using ajax. Following code is working but the result ( Nilantha Ruwan Nimal Shamitha Alex) is just display and disappears.

Index.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    </head>
    <body>
    <div class="container" style="margin-top:50px";>
    <form >
    <div class="form-group">
    <input type="text" id="name" class="form-control" placeholder="Enter Name....">
    </div>
    <div class="form-group">
    <button class="btn btn-success" id="btn">Enter</button>
    </div>
    </form> 
    <div class="msg"></div>     
    </div>
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $("#btn").click(function(){
    var name = $("#name").val();
    $.post("ajax.php",{ajax_name:name},function(response){
    $(".msg").html(response);
    })
    .fail(function(error){
    alert(error.statusText);
        })
      })
    })
    </script>

</body>
</html>

ajax.php

<?php 

    if(isset($_POST['ajax_name'])){
    $store = array("Nilantha","Ruwan","Nimal","Shamitha","Alex");
    foreach($store as $names) {
    echo $names,"<br>";
     }
    }

?>
2
  • Please try my answer it may help you. Commented Jun 21, 2019 at 12:38
  • If still getting error feel free to share your concern Commented Jun 21, 2019 at 12:43

2 Answers 2

1

Try the following code.

  <script type="text/javascript">
        function submit() {
            jQuery("form").submit(function(e) {
                e.preventDefault();
                var name = jQuery("#name").val();
                jQuery.ajax({
                    type: 'POST',
                    url: 'ajax.php',
                    data: {ajax_name:name},
                    success: function(response) {
                        jQuery(".msg").html(response);
                        jQuery('#name').val('');
      },
      error: function() {
        console.log("Something wrong");
      }
    });});
        }

        jQuery(document).ready(function() {
            submit();
        });
    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. it worked, Do you have any idea why my code didn't work? i got it from a video tuttorial it shows that code works. any way thanks for response
0

I would suggest looking at the network tab of Chrome Devtools and ensuring that the AJAX call isn't running twice. I would think that if the query ran twice but the second one did not have any name attached then it would return blank once the first one had received its response.

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.