我正在尝试使用POST方法将表单数据发送到PHP页面。我想在jquery中读取POST值。以下是代码。
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;
?>
它不起作用。 如何在ajax请求(而不是表单)中执行相同的操作,如下所示:
$.ajax({
type: "POST",
url: "test.php",
data: {username: $('#username').val()},
success: function(serverResponse){
$('#Attach').html(serverResponse);
}
});
答案 0 :(得分:3)
脚本中的错误是你应该在javascript中使用echo命令
CODE:
<script type="text/javascript">
alert('<?php echo $uname; ?>');
</script>
答案 1 :(得分:2)
在使用变量之前,您需要确保变量可用。
将页面发布到您的页面后
<?php
//after post
$uname = isset($_POST['username']) ? $_POST['username'] : '';
?>
<script type="text/javascript">
alert('<?php echo $uname ?>');
</script>
<?php echo $uname; ?>
答案 2 :(得分:0)
在您的Jquery alert('<?php $uname ?>');