0

I have a html form which the user inputs values/selects options. After selecting, these values would be sent to the php script in the server. The script would be retrieving some data from mysql depending on the values the user selected. These values and data from mysql would then be collated together and used in a POST request which would be sent to a mobile device. I would be using curl to do the request.

However, I'm unable to do any POST request. I'm not seeing any results or changes. I tried doing echo in my php script but it's not printing out on the html form even if I used GET

Where did I go wrong in my code?

.html

<script type="text/javascript">

    function doSubmit() {

        var env = document.frm.Env.value;
        var appname = document.frm.appname.value;
        var target = document.frm.target.value;
        var messages = document.frm.messages.value;

        var strURL = "sendMessage.php?Env=" +env+ "&appname=" +appname+ "&target=" +target+ "&Message=" +messages;

        xmlhttp.open("GET",strURL,true);
        xmlhttp.send(null);

    }
</script>

</head>

<body>

    <form action="">
        Environment: <div id="Env">
            <select name="customers" onchange="showApplications(this.value)">
                <option value="Environment">Select an environment</option>
                <option value="S">Sandbox</option>
                <option value="P">Production</option>
            </select>
        </div>

        </br>

        Application Name: <div id="appname">
            <select name="appname" onchange="showTargets(this.value)">
                <option value="">Select application</option>
            </select>
        </div>

        </br>

        Target Device : <div id="target">
            <select name="target">
                <option>Select Device OS</option>
            </select>
        </div>
        </br>
        Message: <div id="messages"><input type="text" name="Message" maxlength="200" size="50"></input></div>

        </br>

        <input type = "button" value="Send" onclick="doSubmit();">
    </form>

    </br>

</body>
</html>

.php

<?php
$env      = mysql_real_escape_string($_POST["env"]);
$appname  = mysql_real_escape_string($_POST["appname"]);
$target   = mysql_real_escape_string($_POST["target"]);
$messages = mysql_real_escape_string($_POST["messages"]);


$conn = mysql_connect("127.0.0.1:3306", "root", "");
mysql_select_db("PushApplication", $conn);

if ($target == "All") {
    echo "Hi";
} elseif ($target == "Apple") {
    echo "Apple";
    $query  = "select deviceID from Device where DeviceType='$target'";
    $result = mysql_query($query);

    $num = mysql_numrows($result);

    $i = 0;
    while ($i < $num) {
        $myResult = mysql_result($result, $i);
        $url      = "http://10.28.68.28:8899/?Env='$env'&AppName='$target'&Token='$myResult'&Message=%7B%22aps%22%3A%7B%22alert%22%3A%22'$messages'%22%7D";
        echo $url;
        curl $url
        ++$i;
    }
}

else
    echo "Hi!";
?>

</select>
4
  • what is xmlhttp? is there something missing from your <script> tag? Commented Jan 26, 2012 at 7:56
  • Have you tried <form action="" method="post">? Commented Jan 26, 2012 at 7:56
  • Picked code confuse, can you refactor and share it. Commented Jan 26, 2012 at 8:03
  • the xmlhttp is an example I got from using ajax. I have removed the codes at the top as they were not relevant in the question. Commented Jan 26, 2012 at 8:05

3 Answers 3

4

You should use mysql_real_escape_string AFTER mysql_connect

in other words: you should already have a connection to mysql server by time you call mysql_real_escape_string or it will return false + warning and false is evaluated to an empty string, that's why you don't get any real values at the backend in your code.

UPDATE: it's noticed in the documentation to mysql_real_escape_string at php.net for the second parameter of the function (which is link_identifier (mysql connection resource)):

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated

Ok, here are some more problems with your code, to fix that do the following:

html:

    <form name='frm'...

js:

    var env = document.frm.customers.value;
    ...
    var messages = document.frm.Message.value;

php:

Replace all $_POST with $_GET

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

4 Comments

Agreed, mysql_real_escape_string is an SQL related function that will only work if you're connected to the database.
Hi, I have changed the position of it. But couldn't receive any response. Removing mysql_real_escape_string didn't help either. Any way I could print out my variables to check their contents?
Sure! You can log them into a file, open debugging perspective in your browser (any modern browser usually shipped with some kind of developer toolbar, I personally like chrome) to see what is returned by the backend, you could console.log the results of your ajax query... Warning: console is usually added by developers of developer toolbars for browsers, so it's not a "native" js object (this is not in ECMAScript).
OH! I got it... you send data with GET method and trying to receive with POST ;) Change $_POST to $_GET in the php file or change GET to POST in the js. So, you actually did 2 mistakes :D
2

Javascript accesses document.frm, but the form is not defined with name 'frm'. I guess this is where javascript stops and does not fire the xhr thing.

1 Comment

+1 for pointing out a possible problem. However still does not work. Thanks anyway.
1

You are not outputing anything from your AJAX request. If you are using XMLHttpRequest, you need to define onreadystatechange() method of it and, there, output result in some conditions. But, instead, I advice you to use something like jQuery Ajax, it is much easier and supports huge variety of browsers.

1 Comment

Oh yea. Thats right. Have added this in and at least now it's called the php script.

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.