0

I was recently doing a PHP web-app, which turns out needs AJAX to display temporary and permanent results without reloading the page or redirecting to another page. Just simply update.

So I have a form on my index, where it collects search terms:

<form action="search.php" method="post">
<label for="fname" id="label">Enter search terms:</label>
<br>
<input type="text" id="fname" name="search"><br>
<input type="submit" id="submit">

Then I have it take it to my PHP script, which then processes the search terms and in theory should display them on the same page just in the other paragraph with something like this which is permanent:

echo 'Selected search terms: '. $terms. ".<br>
 Search terms found: ".$termc."." ;

While my PHP script is working, I display a permanent "Loading..." and when it finishes it should display "Done." replacing the "Loading..." text.

Would anyone know how I could implement this with AJAX? How could I talk to PHP?

3
  • 3
    your javascript code? Commented Oct 6, 2017 at 6:03
  • You should try something before asking for help. Do a little Google search and you'll see tons of result about your question. Commented Oct 6, 2017 at 6:06
  • Think about using php function htmlspecialchars() to prevent html code injection in your result display ... Commented Oct 6, 2017 at 6:20

3 Answers 3

1

use the below code to help display data with out refresh the page.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>New User Registration</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
    <div id="newUserHld">
        <form name="serarch_frm" method="post" id="serarch_frm">
        <label for="fname" id="label">Enter search terms:</label>
        <br>
        <input type="text" id="fname" name="search"><br>
        <input type="submit" id="submit" onclick="formSubmit(); return false;" >
            </form>
    </div>
    <div id="success">
    </div>
</body>
    <script>

function formSubmit(){

            $.ajax({
                type:'POST',
                url:'search.php',
                data:$('#serarch_frm').serialize(),
                    success:function(response){
                        $('#success').html(response);
                    }
                });



            return false;
        }


    </script>
</html>

create the search.php file and put this code.

 <?php 
echo 'Selected search terms:'.$_POST['search'];
exit;
?>

check image

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

Comments

0

you need to write js code something like this

(function($){
    $(document).ready(function(){
        $("form").submit(function(e){
            $( ".result" ).html( "Loading.." );
            e.preventDefault();
            // ajax call to your php file
            $.post( "file.php", function( data ) {
                $( ".result" ).html( data );
            });
        });
    });
})(jQuery);

Comments

0

Please follow below step.

1) You can put form tag like this <form onsubmit="submitFormData()">

2) Create javascript function.

3) function submitFormData(){ var form_data ='search='+$('#fname').val();$("#loading").show(); $.ajax({url: "yourphpfile.php",type: "POST",data: form_data,success: function (res){$("#loading").hide();});}

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.