0

Below is my code which always give me return true so my action is submitted. Can any one help me to solve this problem. After getting response from ajax it get true even if I write return false.

<?php
    if(isset($_POST['submit'])){
        die("lokesh");
    }
    ?>
    <html>
        <head>
            <script src="js/jquery-1.8.2.min.js"></script>
            <script>
                function checkjs(){
                    if(document.getElementById('a').value==""){
                        alert("insert a");
                        return false;
                    }
                    if(document.getElementById('b').value==""){
                        alert("insert b");
                        return false;
                    }
                    if(document.getElementById('c').value==""){
                        alert("insert c");
                        return false;
                    }
                    var companyname = document.getElementById('b').value;
                    var username = document.getElementById('c').value;
                    jQuery.ajax({
                type: "POST",           
                dataType: "json",   
                url:'checkexisting.php',
                data:{
                    companyname: companyname,username: username,
                },
                success:function(response) {
                                alert("response"+response);
                                return false;
                }
                });                
                }
            </script>
        </head>
        <body>
            <form action="#" method="post" name="form">
                <input type="text" id="a">
                <input type="text" id="b">
                <input type="text" id="c">
                <input type="submit" name="submit" onclick="return checkjs();">
            </form>
        </body>
    </html>

Below code is for checkexisting.php

<?php
$companyname = $_REQUEST['companyname'];
$username = $_REQUEST['username'];
$response = $companyname.$username;
echo json_encode($response);
?>
LOKESH
  • 1,303
  • 1
  • 16
  • 29
  • on which condition you want to return true and on which condition you want to return falsE? – Dhara Parmar May 17 '16 at 10:36
  • have you refreshed your page properly? Like CTRL+F5? – Jesper Højer May 17 '16 at 10:37
  • AJAX is async in nature..so your script will return immediately even before the response is received – Sandeep Nayak May 17 '16 at 10:39
  • You'll need to `return false` immediately instead of inside the callback. If there's some condition upon which you wanted the form to submit, then just manually submit it. –  May 17 '16 at 10:41
  • You can return the Json Data from php as `json_encode(array("result"=>true)` and in javascript part `var parseData = JSON.parse(response)` and check `alert(parseData.result)` – Sachin Aryal May 17 '16 at 10:42
  • condition doesn't matter but it always return true – LOKESH May 17 '16 at 10:44
  • My condition is complicated but this is the demo for that condition. – LOKESH May 17 '16 at 10:45
  • In my condition I have to validate all field and then check whether user enter username and company_name combination is present in database or not and then I have to confirm whether previous record delete or not? – LOKESH May 17 '16 at 10:49
  • Please @squint above comment contain my requirement. Please suggest how to solve this problem. – LOKESH May 17 '16 at 11:00
  • I already did. Do your field validation, and then do a `return false;` before you send the `$.ajax()`. Then in the callback, manually submit the form if it's ready to go. This is done by getting the `form` element and calling `.submit()`. However, it would seem like you could just submit immediately without the `AJAX`, and do your checks on the server at that time. –  May 17 '16 at 11:10
  • @squint show alternative code in reply. – LOKESH May 17 '16 at 12:20
  • Remove all of the `return false;` and put one `return false;` at the top of the `checkjs` function. Then inside the `success:` callback, put `document.forms.form.submit()` *(where `form` is the `name` of your form)* if the `response` was acceptable. –  May 17 '16 at 12:26
  • If I put return false at the top of checkjs function then this will stop working. – LOKESH May 17 '16 at 12:29

0 Answers0