Parsa Yazdani
Parsa Yazdani

Reputation: 348

Pass javascript prompt input to PHP variable

So I am working on a code signing system for iOS. I need a user's UDID before they can access the website. How can I pass the javascript prompt input to a php variable.

I have tried posting the variable back to the same page.

<?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.');
}
?>

What I need it to do is set $udid (PHP) to what the user entered in either the prompt or the input form.

Upvotes: 0

Views: 575

Answers (2)

Razor
Razor

Reputation: 2641

Change the data attribute to the following,

data: {
    udid: udid
},

Upvotes: 1

Robin Zigmond
Robin Zigmond

Reputation: 18249

reposting my comment as an answer (with a little more detail):

You should have data: {udid: udid} rather than data: udid. The documentation says that data should be on "object, string or array", but it only mentions a string in the explicit case that it's a query string (eg ?key1=value1&key2=value2). By passing it as an object as shown then you ensure that the PHP backend will be able to access $_POST['udid'] and it will have the intended value.

Note: this object can be abbreviated as just data: {udid} if you're using ES6.

Upvotes: 1

Related Questions