run different script when check box is checked

不羁岁月 提交于 2019-12-11 04:34:00

问题


Ok so here's the deal:

<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.


回答1:


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.

In JavaScript

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

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

checkboxObj.id

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

In PHP

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.

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.

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.

Another way

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.

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


来源:https://stackoverflow.com/questions/18832636/run-different-script-when-check-box-is-checked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!