简体   繁体   English

PHP-脚本未执行

[英]PHP - script not being executed

I have a html form which the user inputs values/selects options. 我有一个HTML表单,用户可以输入值/选择选项。 After selecting, these values would be sent to the php script in the server. 选择之后,这些值将被发送到服务器中的php脚本。 The script would be retrieving some data from mysql depending on the values the user selected. 该脚本将根据用户选择的值从mysql检索一些数据。 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. 然后,将来自mysql的这些值和数据整理在一起,并用在POST请求中,该请求将发送到移动设备。 I would be using curl to do the request. 我将使用curl来执行请求。

However, I'm unable to do any POST request. 但是,我无法执行任何POST请求。 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 我尝试在PHP脚本中执行echo ,但是即使使用GET也无法在html表单上打印出来

Where did I go wrong in my code? 我的代码在哪里出错?

.html .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文件

<?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>

You should use mysql_real_escape_string AFTER mysql_connect 您应该在mysql_connect 之后使用mysql_real_escape_string

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. 换句话说:在您调用mysql_real_escape_string时,您应该已经与mysql服务器建立连接,否则它将返回false +警告,并且false被评估为空字符串,这就是为什么您在代码的后端未获得任何实际值的原因。

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)): UPDATE :在文档中注意到该函数的第二个参数(即link_identifier(mysql连接资源))位于php.net mysql_real_escape_string中:

The MySQL connection. MySQL连接。 If the link identifier is not specified, the last link opened by mysql_connect() is assumed. 如果未指定链接标识符,则假定使用mysql_connect()打开的最后一个链接。 If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. 如果找不到这样的链接,它将尝试创建一个链接,就好像没有参数调用mysql_connect()一样。 If no connection is found or established, an E_WARNING level error is generated 如果未找到或建立连接,则生成E_WARNING级错误

Ok, here are some more problems with your code, to fix that do the following: 好的,这是您的代码中的一些其他问题,要解决这些问题,请执行以下操作:

html : html

    <form name='frm'...

js : js

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

php : 的PHP

Replace all $_POST with $_GET 将所有$_POST替换$_POST $_GET

Javascript accesses document.frm , but the form is not defined with name 'frm'. Javascript访问document.frm ,但是该表单未使用名称'frm'定义。 I guess this is where javascript stops and does not fire the xhr thing. 我猜这是javascript停止并且不会触发xhr的地方。

You are not outputing anything from your AJAX request. 您没有从AJAX请求中输出任何内容。 If you are using XMLHttpRequest , you need to define onreadystatechange() method of it and, there, output result in some conditions. 如果使用XMLHttpRequest ,则需要定义它的onreadystatechange()方法,并在某些情况下输出结果。 But, instead, I advice you to use something like jQuery Ajax , it is much easier and supports huge variety of browsers. 但是,相反,我建议您使用jQuery Ajax之类的东西,它要容易得多,并且支持各种浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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