当用户将记录保存到数据库时,我检查了已保存数据的记录。如果记录已存在于数据库中,请询问用户是否使用消息框覆盖或取消。我想让用户响应OK或取消。
首先,我在运行时从php调用js函数。 JS函数显示确认消息并将返回值传递给隐藏文本框。这部分工作正常。
但是,我无法将文本框值传递给php变量。
<script>
function confirmOverwrite() {
var overwriterec = "";
if(confirm("Do you want to overwrite the record? Click on OK to overwrite.Click Cancel to skip."))
overwriterec="ok";
}
else
{
overwriterec="cancel";
}
document.getElementById("hiddenVal").value = overwriterec;
}
</script>
&#13;
<form action="#" method="post">
<div>
<button type="submit" name="Save"> Save data</button>
<input type="text" style="display:none" id="hiddenVal" name="overwrite" />
</div>
</form>
<?php
if(isset($_POST["Save"])){
$Sql = "SELECT * FROM maintable WHERE userid='$userid'";
$result = mysqli_query($db, $Sql);
if (mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
echo '<script type="text/javascript">',
'confirmOverwrite();',
'</script>'; //can call the function and pass the OK or Cancel value to hidden text box.
if((isset($_POST['overwrite'])) && !empty($_POST['overwrite']))
{
$Confirmation = $_POST['overwrite']; //I can't pass the hidden text box value to php variable.
echo $Confirmation;
}
if ($Confirmation == "ok")
{
// update the record
}
}
}
}
?>
&#13;
答案 0 :(得分:0)
您需要在提交之前调用该函数。 我添加了一个隐藏价值的警报。
if(isset($_POST["Save"])){
if((isset($_POST['overwrite'])) && !empty($_POST['overwrite']))
{
$Confirmation = $_POST['overwrite']; //I can't pass the hidden text box value to php variable.
echo $Confirmation
}
}
function confirmOverwrite() {
var overwriterec = "";
if (confirm('Do you want to overwrite the record? Click on OK to overwrite.Click Cancel to skip.')) {
overwriterec="ok";
} else {
overwriterec="cancel";
}
document.getElementById('hiddenVal').value = overwriterec;
alert(document.getElementById('hiddenVal').value);
}
&#13;
<form action="#" method="post" onsubmit="confirmOverwrite()">
<div>
<input type="hidden" id="hiddenVal" name="overwrite" />
<button type="submit" name="Save"> Save data</button>
</div>
</form>
&#13;
答案 1 :(得分:0)
客户端代码:
<html>
<head>
<title>Client Side</title>
</head>
<body>
<script>
function savedata(confirm){
httpRequest=new XMLHttpRequest();
httpRequest.open("POST", "savescript.php",true);
httpRequest.onreadystatechange=saved;
httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
httpRequest.send("confirm="+confirm);
}
function saved(){
readyState=httpRequest.readyState;
if(readyState!=4) return;
httpStatus=httpRequest.status;
if(httpStatus!=200) return;
var phpResponse=httpRequest.responseText;
if(phpResponse==1){ //ask for confirm if the server send back 1
if(confirm("This record already exists. Are you sure to overwrite this record?")) savedata('yes');
}
else savedata('yes');
}
</script>
<p>The example form: </p>
...Other inputs...<br/>
<input type="button" value="Save" onclick="savedata('no')"/>
</body>
</html>
服务器端代码:
<?php
/***********SEVER SIDE CODE (savescript.php)*************/
//Connection to database (mysqli_connect)
$query=mysqli_query($connection_to_db,"your query to check if the record exists");
$confirm=$_POST['confirm']; //the client side confirmation variable
if($confirm=='yes'){ //if the user has confirmed
//if the record exists update else insert
if(mysqli_num_row($query)==1) mysqli_query($connection_to_db,"your query to update the record");
else mysqli_query($connection_to_db,"your query to insert the record");
}
else{ //if the user did not confirm
if(mysqli_num_row($query)==1) echo("1"); //send 1 this means that the record exists then the client ask for confirm
else echo("0"); //send 0 this means that the record doesn't exists then you can insert without ask confirm
}
?>