简体   繁体   中英

Phone Number Not Inserted Into Database

I'm creating a simple form, which takes a couple of fields as input, runs some AJAX checks and submits to SQL database via PHP. I'm able to insert all other fields EXCEPT the phone number. Here's a snippet of the codes:

HTML ---

<form name="signupform" id="signupform" onSubmit="return false;">
<div>Phone Number: </div>
    <input id="phon" type="text" maxlength="20">
<button id="signupbtn" onClick="signup()">Create Account</button>
    <span id="status" style="color: red"></span>
</form>

JS ---

function signup() {
var status = document.getElementById("status");
var phone = document.getElementById("phon").value;
status.innerHTML = phone; //testing, doesn't display anything
        var ajax = ajaxObj("POST", "index.php"); // accepted
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                if(ajax.responseText != "Succesfully signed-up!"){ ///returned from php file
                    status.innerHTML = ajax.responseText;

                }

            }
        }
        ajax.send("phone="+phone); //shoots variable to php 
}

PHP ---

if(isset($_POST["phone"])) { 
                       $phone = $_POST['phone'];
               echo $phone; //was testing, again, nothing shows 
            $sql = "INSERT INTO users(phone) VALUES('$phone')";
        }
$query2 = mysqli_query($con, $sql); 
echo 'successfully_updated';

NOTE: I tried to check if JS and PHP are receiving the phone number, but it's not displaying anything (other form elements such as name and email are displayed, though, tested that already). But later-on, in the PHP code, it isn't showing any error when checked against "if (isset($_POST["phone"])), it inserts the other elements of the form without trouble. No clue why that's happening, any ideas please? My guess is that since JS doesn't reflect the value, that's where the error lies.

Been searching and trying in vain since hours! Any help would be great, thanks!

status.innerHTML = phone; //testing, doesn't display anything

一定是

document.getElementById("status").innerHTML = phone; 

using jQuery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        phoneNum = $("#phon").val();
        $("#signupbtn").click(function(){
            $.ajax({url:"./index.php",data:{phone:phoneNum},success:function(result){
                alert(result);
            }});
        });
    });
</script>

why not write $query2 = mysqli_query($con, $sql) or die(mysqli_error()); That way, you can see what the error occurs

请尝试这个

 $sql = "INSERT INTO users(phone) VALUES('".$phone."')";

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