复选框被选中时运行不同的脚本 [英] run different script when check box is checked

查看:106
本文介绍了复选框被选中时运行不同的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以这里的交易:

<form enctype="multipart/form-data" class="pdfsub" id="pdfsub"  action="" method="post">
 <td>
 <p>Agent Name:<input type="text" name="agentname" id="agentname" /></p>
  <p>Description: <textarea cols="75" rows="10" draggable="false" name="desc"    value="desc">
 </textarea></p>

 </td>
 <td>
 <p>Current Date: <input type="date" name="date" value="date" /></p>


 <p> Document Name: <select name="pdf" id="pdf" class="pdf" value="pdf">
 <option></option>
   <?php
$x=0;
   if ($handle = opendir('Users/'.$_SESSION['username'].'/uploaded/')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo "<option value=".$entry.">$entry\n</option>";
        }
    }
    closedir($handle);
  }
  ?>
    </select></p>
  <p>Processed: <input type="checkbox" name="options[]" onclick="checkSubmit(this,    'mySubmit')" value="y" />    Incomplete: <input type="checkbox" name="options[]"    onclick="checkSubmit(this, 'mySubmit')" value="y" /></p>


<input type="submit" name="submit" value="submit" id="mySubmit" disabled="disabled" />
 </td>
 </form>
 </table>
 <script type="text/javascript">
 function checkSubmit(ele, id) {
x = document.getElementById(id);
if (ele.checked == true) x.disabled = false;
else x.disabled = true;
 }
</script>
<?php
error_reporting(0);
if (isset($_post['submit']['processed']))
{
$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++){
echo "Selected " . $checked[$i] . "<br/>";
}


$host = "host";
$user3 = "user";
$db_name= "dbname";
$pass= "pass";


$con = mysql_connect($host, $user3, $pass);

if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("dbname", $con);
$agent=($_POST['agentname']);
$desc=($_POST['description']);
$date=($_POST['date']);
$pdf=($_POST['filename']);

$sql ="INSERT INTO `documets` (`agentname`, `description`, `date`, `filename`, `numb`) VALUES ('$agent', '$desc', '$date', '$pdf', '1')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error()); 
} 

mysql_close($con);




$srcfile= ('Users/'.$_SESSION['username'].'/uploaded/'.$pdf);
$dstfile= ('Users/'.$_SESSION['username'].'/processed/'.$pdf);
copy($srcfile, $dstfile);
unlink($srcfile);
}

?>
<?php
error_reporting(0);
if (isset($_post['submit']['incomplete']))
{



$host = "host";
$user3 = "user";
$db_name= "dbname";
$pass= "pass";

$con = mysql_connect($host, $user3, $pass);

if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("dbname", $con);
$agent=($_POST['agentname']);
$desc=($_POST['description']);
$date=($_POST['date']);
$pdf=($_POST['filename']);
$sql ="INSERT INTO `documets` (`agentname`, `description`, `date`, `filename`, `numb`) VALUES ('$agent', '$desc', '$date', '$pdf', '1')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error()); 
} 

mysql_close($con);




$srcfile= ('Users/'.$_SESSION['username'].'/uploaded/'.$pdf);
$dstfile= ('Users/'.$_SESSION['username'].'/incomplete/'.$pdf);
copy($srcfile, $dstfile);
unlink($srcfile);

}
?>

我需要以某种方式组合所有这些,以便根据选择哪个复选框是文件的位置但数据仍然提交到数据库。

i need to somehow combine all of this so that depending on which checkbox is selected is where the file goes but the data is still submitted to the database. as of right now its not doing either but thats an easy fix i just need the correct structure.

推荐答案

你的问题不是很复杂,清楚,但就我所见,你要问如何做不同的事情,取决于选择哪个复选框,对不对?我不知道你是否指的是如何在JavaScript或PHP中这样做,所以我将添加两者。

Your question is not very clear, but as far as I can see you are asking how to do different things depending on which checkbox is selected, right? I am not sure if you are referring to how to do this in JavaScript or PHP, so I will add both.

也许给两个复选框不同的id会解决你的问题。然后,您可以通过例如使用以下命令检查哪个复选框被选中:

Maybe giving both checkboxes a different id will solve your problem. You can then find out which checkbox is checked by for example using:

getElementById("checkboxId").checked //either true or false

如果你只想知道被点击的复选框,你可以检查它的id: / p>

If you only want to know which checkbox was being clicked, you can check it's id:

checkboxObj.id

其中,checkObj是您在复选框的onclick属性中传递的对象。

Where checkObj is the object you pass in the onclick attribute of your checkboxes.

在PHP中,当复选框被选中时,它的名称 - 值对被提交。如果没有选中,它不会出现在提交到服务器的数据中。通过检查某个复选框的值是否出现在提交到服务器的数据中,您可以检查它是否已被选中。在你的case,你可以检查它的值是否出现在'选项'数组中。

In PHP, when a checkbox is checked, it's name-value pair is submitted. If its not checked, it will not appear in the data submitted to the server. By checking if the value for a certain checkbox appears in the data submitted to the server, you can check if it has been checked. In your case, you could check if it's value appears in the 'options' array.

但是,你使用相同的值(y ,所以你看不到它们之间的区别。通过给它们不同的值(例如x和y),您将能够看到选中了哪个复选框。

You are, however, using the same value ("y") for both checkboxes, so you cannot see the difference between them. By giving them different values (like "x" and "y"), you will be able to see which checkbox has been checked.

然后,$ _GET数组例如,当选中两个复选框时,像这样:

The $_GET array will then look like this when, for example, both checkboxes are checked:

$_GET["option"][0] === "x";
$_GET["option"][1] === "y";

您也可以像在代码中已经做的一样循环。

You can also loop over them like you are already doing in your code.

不需要在一个数组中提交复选框。您也可以选择为它们使用不同的名称。在这种情况下,您将能够通过使用$ _GET数组中的相应索引找到它们。

It is not needed to submit your checkboxes all in one array. You can also choose to use different names for them. In that case, you will be able to find them both by using the corresponding index in the $_GET array.

假设复选框的名称是check1 check2,那么你可以使用下面的代码来检查它们是否被检查:

Suppose that the names for the checkboxes are "check1" and "check2", then you can use the following code to check whether they are checked:

isset($_GET['check1']) //True if "check1" is checked
isset($_GET['check2']) //True if "check2" is checked

这篇关于复选框被选中时运行不同的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆