0

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!

Govind Singh
  • 15,282
  • 14
  • 72
  • 106
VGupta
  • 109
  • 8
  • are you using integer to save phone number? – Arijit Mukherjee Jun 06 '14 at 11:46
  • So if the `$phone` variable is empty, how do you expect the database column to be populated? – marekful Jun 06 '14 at 11:46
  • Debug using firebug. What values are being set via AJAX call ? – Rahul Gupta Jun 06 '14 at 11:48
  • 1
    "Can you find the bug" questions [are not good questions for Stack Overflow](http://meta.stackoverflow.com/questions/253787/are-there-legitimate-fix-my-code-questions?cb=1#253788). Make sure you provide a brief, but **specific statement of the problem**, telling us precisely what is wrong. "It doesn't work" is not a problem statement. – John Conde Jun 06 '14 at 11:48
  • FYI, you are wide open to [SQL injections](http://stackoverflow.com/q/60174). – John Conde Jun 06 '14 at 11:49
  • @CyberDude no. He assigns phon to variable phone here var phone = document.getElementById("phon").value; – Xatenev Jun 06 '14 at 11:50
  • Please change query as like this $sql = "INSERT INTO users(phone) VALUES('".$phone."')"; – Elby Jun 06 '14 at 11:57
  • @ArijitMukherjee yeah I've tried integer, varchar, etc, but that's not where the problem lies, it's not picking the phone number to begin with. – VGupta Jun 06 '14 at 12:02
  • @MarcellFülöp - that's the point. I am missing something at JS, perhaps, and I am unable to understand what. – VGupta Jun 06 '14 at 12:04
  • @JohnConde - I appreciate your concerns, however I have done my testing before submitting my query, I don't think it's a simple "find the bug" issue, and neither is SQL injection my concern at the moment, I'll get to that once my basic code is running. Thanks anyways. :) – VGupta Jun 06 '14 at 12:07
  • If I understand you well, the phone number is not picked in js. If that's the case, please change your question to reflect that (php et database mentions aren't relevant) – merours Jun 06 '14 at 12:11

4 Answers4

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

must be

document.getElementById("status").innerHTML = phone; 
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
  • Hi Govind, thanks, but my bad, I missed adding the line - var status = document.getElementById("status"); So that's not the case. Adding the line to the code. Thanks. – VGupta Jun 06 '14 at 11:55
1

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>
Cristofor
  • 2,077
  • 2
  • 15
  • 23
0

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

Debanjan
  • 99
  • 9
0

Please try this

 $sql = "INSERT INTO users(phone) VALUES('".$phone."')";
Elby
  • 1,624
  • 3
  • 23
  • 42
  • Thanks but all other insertions into the database work fine with the '$phone', '$name', etc, way, that isn't the problem. – VGupta Jun 06 '14 at 12:09
  • @user3714817 because single quotes is not accepted in double quotes – Elby Jun 06 '14 at 12:11