I have a simple javascript alert box that lets a customer enter some info.
But I need to pass whatever they type to a php variable for later use, and I can't quite figure it out.

Here is what I did:

<html>
<head>
<title>Writing Names</title>
<script language="JavaScript">
var viewer_name = prompt ("Please enter your name.","Type Name Here");
</script>
</head>
<body bgcolor="#ffffff">

<? $test= ?>
<script language="JavaScript">
document.write(viewer_name);
</script>
<?php 
echo $test;
?>

As you can see, I have attempted to pass the viewer_name field to the variable: $my_name;

However, I get nothing on the page, and the logs tell me : parse error, unexpected ';' on the same line as <? $test= ?>

Any ideas how to pass a js variable to PHP?

    Easiest way is via a hidden form field

    <?php
    
    if (isset($_POST['test'])) echo 'PHP HERE : Name is : ' . $_POST['test'];
    
    ?>
    <html>
    <head>
    <title>Writing Names</title>
    <script language="JavaScript">
        function submitform(form)
        {
            var viewer_name = prompt ("Please enter your name.","Type Name Here");
            form.test.value = viewer_name;        // put value in hidden form field
            form.submit();
    
    }
    </script>
    </head>
    <body bgcolor="#ffffff">
    
    <form name='form1' method='post'>
    <p>click submit to view name</p>
    <input type='hidden' name='test' value=''>
    <input type='button' value='Submit' onclick='submitform(this.form)'>
    </form>
    
    </body>
    </html>
      Write a Reply...