将S_POST数据传递给jquery

I am trying to send a form data using POST method to a PHP page. I want to read that POST value in jquery. Below is the code.

HTML:

<form method="post" action="test.php">
  <input name="username" id="username"></input>
  <input type="button"></input>
</form>

PHP:

<?php
<script type="text/javascript">
      alert(<?php $uname ?>);
</script>

$uname = $_POST["username"];
echo $uname;
?>

Its not working. How can I do the same in an ajax request (instead of a form) like below:

$.ajax({
    type: "POST",
    url: "test.php", 
    data: {username: $('#username').val()}, 
    success: function(serverResponse){
        $('#Attach').html(serverResponse);
    }
});

You need to make sure the variable is available before you use it.

Once the page is posted to your page

<?php
    //after post 
    $uname = isset($_POST['username']) ? $_POST['username'] : '';
?>

<script type="text/javascript">
      alert('<?php echo $uname ?>');
</script>


<?php echo $uname; ?>

The bug in your script is you should use echo command in javascript

CODE:

<script type="text/javascript">
      alert('<?php echo $uname; ?>');
</script>

use in your Jquery alert('<?php $uname ?>');