简体   繁体   中英

Post data from HTML input form with PHP action file without refreshing the page

I have an HTML input form, and a PHP file. Now I am trying to execute the PHP file and return the result without refreshing the page. So, I've used javascript to do that. But now after using javascript, the $_POST[uid] is unable to get the ID that user entered, so the PHP file returns the else condition.

I have this HTML form:

<form method="post" action="test.php">
    <span>
        Sign in System
    </span>
    <div data-validate = 
 "ID is required">
        <input id="uid" type="text" name="uid" 
 placeholder="Please enter your ID number">
        <span class="focus-input100"></span>
        <span class="symbol-input100">
            <i class="fa fa-lock" aria-hidden="true"></i>
        </span>
    </div>
                
    <input type="button" id="searchForm" 
onclick="SubmitForm();" value="Signin" />

and this is the used javascript code:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function SubmitForm() {
    var uid= $("#uid").val();
    $.post("test.php", { uid: uid},
        function(data) {
            alert("Sorry.. " + data);
        });
    }
</script>

and This is my PHP Code:

session_start();
$uid = $_SESSION['uid'] = $_POST['uid'];
$first7 = substr($uid, 0, 8);
$ids=Array(Some Array Values)
foreach ($ids as $id) {
    if (substr($id, 0, 8) === $first7){
        header("Location: success.php");
    }
    else{ 
        echo "Entered ID is Wrong"; break;
    }
}

Now the result always give me the else condition which shows an alert box says: Entered ID is wrong.. which doesn't even able to check if it is correct and redirect to success.php

Any Idea??

var idd = $("#uid").val();

you dont have a idd name in the input, its uid

and in php

session_start();
$uid = $_SESSION['uid'] = $_POST['idd'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM