如何在PHP中使用JavaScript另存为功能?
How to use JavaScript in PHP save as function?
我是 PHP 的新人。我有一个表单,它有不同的提交按钮,如保存、编辑、取消和另存为。现在我想要当用户单击另存为按钮时 java 脚本是 运行 并检查文件名。如果 db 中已经存在同名文件,则显示警告消息 "Please Change file name" 如果用户更改文件名,则执行按钮代码。
我使用按钮代码
<input type="submit" name="submit" value="Save as" onclick="show_confirm()" />
<input type="submit" name="submit" value="Make a copy" />
并在 PhP
if($_POST['submit'] == 'Make a copy') {
$action = "copy";
} elseif($_POST['submit'] == 'Save as') {
$action = "save as";
}
并且可能是 java 这样的脚本
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Please Change File Name!?");
if (r==true)
{
}
else
{
}
}
</script>
我的表格是这样的
现在我正在尝试编写我的 Java 警报消息脚本 你能帮我解决我的问题吗?
不要使用相同名称的提交按钮,而是使用这些
<input type="button" value="Save as" onclick="show_confirm(1)" />
<input type="button" value="Make a copy" onclick="show_confirm(2)" />
<input type="hidden" value="" id="what_to_do" name="what_to_do" />
enter code here
并在您的脚本中
<script type="text/javascript">
function show_confirm(a)
{
if (a == 1) {
$("#what_to_do").val("save_as");
}
if (a == 2) {
$("#what_to_do").val("make_copy");
}
// now submit form
$('#myForm').submit();
}
</script>
而在你PHP身边
<?php
// $_POST['what_to_do'] is actually this one - <input type="hidden" value="" id="what_to_do" name="what_to_do" />
if ($_POST['what_to_do'] == 'make_copy') {
$action = "copy";
} elseif ($_POST['what_to_do'] == 'save_as') {
$action = "edit";
}
<form action="program.php" method="post">
Default value in this case: Maurize
<input type="text" name="filename" placeholder="Username"/><br>
<input type="submit" name="submit" value="Save as"/>
<input type="submit" name="submit" value="Make a copy" />
</form>
现在您可以检查姓名:
<?php
$oldUsername = "Maurize"; //Will be triggered out of database
$newUsername = $_POST["filename"]; //This is from html
switch ($_POST['submit']) {
case 'Save as':
if ($oldUsername == $newUsername){ //If the name is already present
echo('<script>alert("Please change your filename because it already exists"); window.location.href = "index.html";</script>');
}
else{
echo('<script>alert("Creating your filename was succesfully"); window.location.href = "index.html";</script>');
}
break;
case 'Make a copy':
echo "make a copy";
break;
}
?>
现在我们的标准表格位于顶部。如果我们提交这个,我们的 program.php 会像下面这样被调用。如果该名称已经存在,我们会提醒他并将他送回页面。
我是 PHP 的新人。我有一个表单,它有不同的提交按钮,如保存、编辑、取消和另存为。现在我想要当用户单击另存为按钮时 java 脚本是 运行 并检查文件名。如果 db 中已经存在同名文件,则显示警告消息 "Please Change file name" 如果用户更改文件名,则执行按钮代码。 我使用按钮代码
<input type="submit" name="submit" value="Save as" onclick="show_confirm()" />
<input type="submit" name="submit" value="Make a copy" />
并在 PhP
if($_POST['submit'] == 'Make a copy') {
$action = "copy";
} elseif($_POST['submit'] == 'Save as') {
$action = "save as";
}
并且可能是 java 这样的脚本
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Please Change File Name!?");
if (r==true)
{
}
else
{
}
}
</script>
我的表格是这样的
现在我正在尝试编写我的 Java 警报消息脚本 你能帮我解决我的问题吗?
不要使用相同名称的提交按钮,而是使用这些
<input type="button" value="Save as" onclick="show_confirm(1)" />
<input type="button" value="Make a copy" onclick="show_confirm(2)" />
<input type="hidden" value="" id="what_to_do" name="what_to_do" />
enter code here
并在您的脚本中
<script type="text/javascript">
function show_confirm(a)
{
if (a == 1) {
$("#what_to_do").val("save_as");
}
if (a == 2) {
$("#what_to_do").val("make_copy");
}
// now submit form
$('#myForm').submit();
}
</script>
而在你PHP身边
<?php
// $_POST['what_to_do'] is actually this one - <input type="hidden" value="" id="what_to_do" name="what_to_do" />
if ($_POST['what_to_do'] == 'make_copy') {
$action = "copy";
} elseif ($_POST['what_to_do'] == 'save_as') {
$action = "edit";
}
<form action="program.php" method="post">
Default value in this case: Maurize
<input type="text" name="filename" placeholder="Username"/><br>
<input type="submit" name="submit" value="Save as"/>
<input type="submit" name="submit" value="Make a copy" />
</form>
现在您可以检查姓名:
<?php
$oldUsername = "Maurize"; //Will be triggered out of database
$newUsername = $_POST["filename"]; //This is from html
switch ($_POST['submit']) {
case 'Save as':
if ($oldUsername == $newUsername){ //If the name is already present
echo('<script>alert("Please change your filename because it already exists"); window.location.href = "index.html";</script>');
}
else{
echo('<script>alert("Creating your filename was succesfully"); window.location.href = "index.html";</script>');
}
break;
case 'Make a copy':
echo "make a copy";
break;
}
?>
现在我们的标准表格位于顶部。如果我们提交这个,我们的 program.php 会像下面这样被调用。如果该名称已经存在,我们会提醒他并将他送回页面。