将javascript提示输入传递给PHP变量

时间:2019-04-23 11:54:06

标签: javascript php

因此,我正在开发iOS的代码签名系统。我需要用户的UDID才能访问该网站。如何将javascript提示输入传递给php变量。

我尝试将变量发布回同一页。

<?php
    $udid = $_POST['udid'];
    if(empty($udid)){
        $udid = file_get_contents("saves/" . $ip . ".txt");
    }
    if(empty($udid)){
?>
<script>
var udid=prompt("Please enter your UDID");
$.ajax(
{
    type: "POST",
    url: "app.php",
    data: udid,
    success: function(data, textStatus, jqXHR)
    {
        console.log(data);
    }
});
</script>
<?php
    }
if( strpos(file_get_contents("cert1.txt"),$udid) !== false) {
            echo "Device status:<br><span class='badge badge-dark'>Signed</span><br>";
            echo "Signed on cert:<br><span class='badge badge-dark'>1</span><br>";
        } else {
            $t = ' ' . time();
            echo "<p>Device status:<br><span class='badge badge-dark'>Unsigned</span><br>You are now<br>on the waitlist</p><script>alert(\"Your device isn't approved yet. We have added you to the waitlist. Check back soon.\");</script>";
            $txt = $_GET['udid'] . $t;
            $myfile = file_put_contents('notsigned.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
            header("Location: notsigned.php");
        }
?>
<br>
<a href="http://get.udid.io" style="text-decoration:none;font-family:arial;font-size:15px;color:#fff;padding:8px;border-radius:5px;background-color:blue;margin-bottom:5px;">Get your udid</a>
<br><br>
<form class='form-horizontal well' action='#' method='post'>
    <input type='text' name='udid' class='input-large' size="9" border="2" placeholder="udid" value='<?= $udid ?>'>
    <button type="submit" id="submit" style="text-decoration:none;font-family:arial;font-size:15px;color:#fff;padding:8px;border-radius:5px;background-color:springgreen;margin-bottom:5px;" class="badge-primary">Save</button>
</form>
<?php
    setcookie("udid", $udid, time()+31536000000, "/");
file_put_contents("saves/" . $ip . ".txt",$udid);
if(empty($udid)){
    alert('You cannot access anything till you enter your udid.');
}
?>

我需要做的是将$ udid(PHP)设置为用户在提示或输入表单中输入的内容。

2 个答案:

答案 0 :(得分:1)

将我的评论重新发布为答案(更详细):

您应该拥有data: {udid: udid}而不是data: udiddocumentation表示data应该在“对象,字符串或数组”上,但仅在明确的情况下提及一个字符串,即它是查询字符串(例如{{ 1}})。通过如图所示将其作为对象传递,则可以确保PHP后端将能够访问?key1=value1&key2=value2并且具有预期的值。

注意:如果您使用的是ES6,则可以将该对象缩写为$_POST['udid']

答案 1 :(得分:1)

data属性更改为以下内容,

data: {
    udid: udid
},