我如何使用php与javascript标签
how can I use php with javascript tag?
我有一个表单。我想在用户点击保存按钮时发出警报。然后,如果用户单击yes,则应该执行一些php代码。我该怎么做呢?我可以在输入标签中使用onclick吗?
<?php if (isset($_POST['save'])){ ?>
<script language="javascript">
if (confirm('It should be reloaded, Are you sure you want to save changes?')) {
<?php $x=$_POST['myName']; ?>
}
else{
<?php $x='nothing' ?>
}
</script>
<?php }
echo $x;
?>
<form method="post" action="">
name:<input type="text" name="myName" />
city:<input type="text" name="city" />
email:<input type="email" name="email" />
<br/>
<input type="submit" name="save" value="save"/>
</form>
将代码改为
<?php
// connection
if ($_POST['t'] == 'execute'){
<?php $x=$_POST['myName']; ?>
}else{
<?php $x='nothing' ?>
}
?>
<form method="post" action="">
name:<input type="text" name="myName" />
city:<input type="text" name="city" />
email:<input type="email" name="email" />
<br/>
<input type="hidden" name="t" value="execute"/>
<input type="submit" name="save" onclick="return confirm('you message')" value="save"/>
</form>
相关文章: