I want form to be loop by how many checkboxes are checked. Like if for each color what sizes are available. But having some issues. For each checkbox I check It's implementing on all color arrays like in the picture what am looking for
form page
<form method="post" action="submit.php">
ID:<input type="text" name="id" />
Name:<input type="text" name="product" />
<div class="form-group fieldGroup">
<div class="input-group">
Color : <input type="text" name="color[]" id="color">
X <input type="checkbox" name="size[]" id="size" value="X" >
XL <input type="checkbox" name="size[]" id="size" value="XL">
S <input type="checkbox" name="size[]" id="size" value="S">
M <input type="checkbox" name="size[]" id="size" value="M">
<span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Add
</div>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="SUBMIT"/>
</form>
submit.php code
if(isset($_POST['submit'])){
$colorArray = $_POST['color'];
$sizeArray = $_POST['size'];
$product = $_POST['product'];
$sl = 1;
$id = $_POST["id"];
$product = $_POST["product"];
if(!empty($colorArray)){
for($i = 0; $i < count($colorArray); $i++){
if(!empty($colorArray[$i])){
foreach ($sizeArray as $x ){
$color = $colorArray[$i];
echo "<h2>" .$sl."> Name : ". $product ." > ".$id ." > ". $color .">" .$x ."</h2>";
$sl++;
}
}
}
}
}
Related
I have added the hidden field in the form and inserting the data into the database. I have 16 fields on my page and If the user enters any value in the field then I have to change the value from 0 to 1 in the input field.
I tried the below code but it's not working.
$(document).ready(function() {
//Check this link
$(".onchangefield").change(function() {
var val = $(this).closest(".onchangefield").find(".haschange").val(1);
//$(".haschange").val(1);
alert(val);
});
});
<form action="process.php" name="employee" method="post">
<input type="text" name="name" class="onchangefield">
<input type="hidden" name="haschange[name]" class="haschange" value="0" />
<input type="email" name="email" class="onchangefield">
<input type="hidden" name="haschange[email]" class="haschange" value="0" />
<button type="submit" name="save">Save</button>
<button type="submit" name="send_for_approval">Send to approval</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
How to change the value in the foreach.
<?php
$i=1;
$arrayName = array('1','2','3');
foreach ($arrayName as $key => $value) {?>
<input type="file" name="slider[]" class="fileupload onchangefield">
<input type="hidden" name="haschange[slider][<?php echo $i;?>]" class="haschange" value="">
<?php $i++; } ?>
You can use .next() to get the reference of next input and change value there .
Demo Code :
$(".onchangefield").change(function() {
var selector = $(this).next() //get next input..
selector.val($(this).val() != "" ? 1 : 0) //depending on value change value of next
});
<form action="process.php" name="employee" method="post">
<input type="text" name="name" class="onchangefield">
<input type="text" name="haschange[name]" class="haschange" value="0" />
<input type="email" name="email" class="onchangefield">
<input type="text" name="haschange[email]" class="haschange" value="0" />
<button type="submit" name="save">Save</button>
<button type="submit" name="send_for_approval">Send to approval</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
i would like input from my user to go into a database, but it is not working. I have a working block of sql code that would insert the data into the database, but i keep getting "undefined" errors when it gets ran.
This is the fully working sql code
<?php
require 'creationlink.php';
$quizname = $_POST['name'];
$quest = $_POST['question'];
$ans1 = $_POST['answer1'];
$ans2 = $_POST['answer2'];
$ans3 = $_POST['answer3'];
$ans4 = $_POST['answer4'];
$correct = $_POST['A'];
//initiates connection with database with creation file
$sql = "INSERT INTO Quiz(question, answer1, answer2, answer3, answer4, correct) VALUES (?, ?, ?, ?, ?, ?)";
if($stmt = mysqli_prepare($conn, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssssss", $quest, $ans1, $ans2, $ans3, $ans4, $correct);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not execute query: $sql. " . mysqli_error($conn);
}
}
else{
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($conn);
}
?>
and this is the code that i would like the sql to apply to, which is just a simple quiz template that iterates depending on the user input.
<html>
<body>
<form id='namennumber' action="includes/dbquestions.php" method="post">
<label for="quizname"><b>Quiz name</b></label>
<input name="quizname" type="text" placeholder="Enter quizname" required>
<div id="myHTMLWrapper">
</div>
</form>
<div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">
<div id="questionform" class="modal">
<label for="question"><b>Enter a question</b></label>
<input name="question" type="text" placeholder="Enter a question" value ="question" required>
<br>
<label for="answer1">Possible answer A</label>
<input type="text" name="answer1" placeholder="Enter a possible answer" value ="answer1"required>
<br>
<label for="answer2">Possible answer B</label>
<input type="text" name="answer2" placeholder="Enter a possible answer" value ="answer2"required>
<br>
<label for="answer3">Possible answer C</label>
<input type="text" name="answer3" placeholder="Enter a possible answer" value ="answer3"required>
<br>
<label for="answer4">Possible answer D</label>
<input type="text" name="answer4" placeholder="Enter a possible answer" value ="answer4"required>
<br>
<h3>Correct answer</h3>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />
<label for="A">A</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />
<label for="B">B</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />
<label for="C">C</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D"/>
<label for="D">D</label>
<br>
<form action="includes/dbquestions.php">
<button style="width:auto;" id="enter" class="btn btn-primary big-btn" >Save</button>
</form>
</div>
</div>
<script>
var wrapper = document.getElementById("myHTMLWrapper");
var number = prompt("Enter the number of questions");
var myHTML = '';
for (var i = 0; i < number; i++) {
myHTML += '<span class="test">Question ' + (i + 1)
myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +
'</span><br/><br/>';
}
wrapper.innerHTML = myHTML
function toggle(cont) {
var cont = document.getElementById('cont');
if (cont.style.display == 'block') {
cont.style.display = 'none';
document.getElementById(ele.id).value = 'Show DIV';
}
else {
cont.style.display = 'block';
document.getElementById(ele.id).value = 'Hide DIV';
}
};
</script>
</div>
</body>
In your HTML, all the <input> elements are outside the <form> and hence you are getting the undefined error. To resolve the error move your <input> elements to the <form> tag. Try replacing your html code as below.
<html>
<body>
<form id='namennumber' action="includes/dbquestions.php" method="post">
<label for="quizname"><b>Quiz name</b></label>
<input name="quizname" type="text" placeholder="Enter quizname" required>
<div id="myHTMLWrapper">
</div>
</form>
<div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">
<div id="questionform" class="modal">
<form action="includes/dbquestions.php"> //moved all the input elements
<label for="question"><b>Enter a question</b></label>
<input name="question" type="text" placeholder="Enter a question" value="question" required>
<br>
<label for="answer1">Possible answer A</label>
<input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>
<br>
<label for="answer2">Possible answer B</label>
<input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>
<br>
<label for="answer3">Possible answer C</label>
<input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>
<br>
<label for="answer4">Possible answer D</label>
<input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>
<br>
<h3>Correct answer</h3>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />
<label for="A">A</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />
<label for="B">B</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />
<label for="C">C</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />
<label for="D">D</label>
<br>
<button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>
</form>
</div>
</div>
<script>
var wrapper = document.getElementById("myHTMLWrapper");
var number = prompt("Enter the number of questions");
var myHTML = '';
for (var i = 0; i < number; i++) {
myHTML += '<span class="test">Question ' + (i + 1)
myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +
'</span><br/><br/>';
}
wrapper.innerHTML = myHTML
function toggle(cont) {
var cont = document.getElementById('cont');
if (cont.style.display == 'block') {
cont.style.display = 'none';
document.getElementById(ele.id).value = 'Show DIV';
} else {
cont.style.display = 'block';
document.getElementById(ele.id).value = 'Hide DIV';
}
};
</script>
</body>
</html>
As a note, you are using the same parameter for action, for both the <form>s in your HTML. See below.
action="includes/dbquestions.php"
Since you are using $_POST in your server side code, you will have to specify method as post for your <form>, as the default method for a <form> is get and not post.
UPDATE
From your errors, that you have posted in the comments, I've combined the 2 forms that you were using.
<html>
<body>
<form id='namennumber' action="includes/dbquestions.php" method="post">
<label for="quizname"><b>Quiz name</b></label>
<input name="quizname" type="text" placeholder="Enter quizname" required>
<div id="myHTMLWrapper">
</div>
<div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">
<div id="questionform" class="modal">
<label for="question"><b>Enter a question</b></label>
<input name="question" type="text" placeholder="Enter a question" value="question" required>
<br>
<label for="answer1">Possible answer A</label>
<input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>
<br>
<label for="answer2">Possible answer B</label>
<input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>
<br>
<label for="answer3">Possible answer C</label>
<input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>
<br>
<label for="answer4">Possible answer D</label>
<input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>
<br>
<h3>Correct answer</h3>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />
<label for="A">A</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />
<label for="B">B</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />
<label for="C">C</label>
<br>
<input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />
<label for="D">D</label>
<br>
<button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>
</form>
</div>
</div>
<script>
var wrapper = document.getElementById("myHTMLWrapper");
var number = prompt("Enter the number of questions");
var myHTML = '';
for (var i = 0; i < number; i++) {
myHTML += '<span class="test">Question ' + (i + 1)
myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +
'</span><br/><br/>';
}
wrapper.innerHTML = myHTML
function toggle(cont) {
var cont = document.getElementById('cont');
if (cont.style.display == 'block') {
cont.style.display = 'none';
document.getElementById(ele.id).value = 'Show DIV';
} else {
cont.style.display = 'block';
document.getElementById(ele.id).value = 'Hide DIV';
}
};
</script>
</body>
</html>
I am having a PHP while loop. Inside my while loop, I am having an HTML form. I am processing that form values through ajax into my PHP which then updates those values in my DB. The problem is whenever I submit that form it only updates the first fetched row in my table. I am also passing the unique Id through my form into ajax but for some reason, only the first row is keep getting updated.
My Php Code:
<?php
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $row) { ?><form>
<input type="text" name="id" value="<?php echo $row['no'] ?>"><br>
<fieldset>
<div>
<label>XYZ Question</label>
</div>
<div>
<input type="radio" name="optradio" value="NO">NO
<input type="radio" name="optradio" value="YES">YES
</div>
</fieldset>
<fieldset>
<div>
<label>XYZ Question</label>
</div>
<div>
<input type="radio" name="optradio1" value="NO">NO
<input type="radio" name="optradio1" value="YES">YES
</div>
</fieldset>
<fieldset>
<div>
<label>XYZ Question</label>
</div>
<div>
<input type="radio" name="optradio2" value="NO">NO
<input type="radio" name="optradio2" value="YES">YES
</div>
</fieldset>
<button type="submit" value="Submit" class="btn">SUBMIT</button>
</form><?php }
My Ajax Code:
$(document).ready(function(){
var launchAjax = function () { // event handler for button click
$.post(
"inbetween.php/",
{
id: $("[name=id]").val(),
question: $("[name=optradio]:checked").val(),
question1: $("[name=optradio1]:checked").val(),
question2: $("[name=optradio2]:checked").val(),
}
);
}
$(".btn").click(launchAjax);
});
My PHP SQL Query
<?php
include 'common.php';
$id = filter_input(INPUT_POST, "id", FILTER_SANITIZE_NUMBER_INT);
$question = filter_input(INPUT_POST, "optradio", FILTER_SANITIZE_STRING);
$question1 = filter_input(INPUT_POST, "optradio1", FILTER_SANITIZE_STRING);
$question2 = filter_input(INPUT_POST, "optradio2", FILTER_SANITIZE_STRING);
function getMark($answer, $mark = 1){
$result = 0;
if($answer == 'YES'){
$result = $mark;
}
return $result;
}
$p = 0;
$p += getMark($question, 1);
$p += getMark($question1, .5);
$p += getMark($question2, 2);
$command1 = "UPDATE rating SET marks = marks + '$c', marks= marks/ totalNumber WHERE no = '$id'";
// prepare and executing
$stmt1 = $dbh->prepare($command1);
$result = $stmt1->execute();
?>
You are using PDO, but your while loop seems to be using the mysqli approach, and if you are selecting multiple rows you use fetchAll, update your code as such:
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $row) {
$id = $row['no'];
}
Update: if you want multiple forms, one for each row, you have to do it this way:
<?php
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $row) { ?><form>
<input type="text" name="id" value="<?php echo $row['no'] ?>"><br>
<fieldset>
<div>
<label>XYZ Question</label>
</div>
<div>
<input type="radio" name="optradio" value="NO">NO
<input type="radio" name="optradio" value="YES">YES
</div>
</fieldset>
<fieldset>
<div>
<label>XYZ Question</label>
</div>
<div>
<input type="radio" name="optradio1" value="NO">NO
<input type="radio" name="optradio1" value="YES">YES
</div>
</fieldset>
<fieldset>
<div>
<label>XYZ Question</label>
</div>
<div>
<input type="radio" name="optradio2" value="NO">NO
<input type="radio" name="optradio2" value="YES">YES
</div>
</fieldset>
<button type="submit" value="Submit" class="btn">SUBMIT</button>
</form><?php }
I have a hidden div I am loading on click of submit button. I also have .notShouldBeBlank on the script for the form. What is the right method so that the php sends results and the hidden div will not load until all required fields are complete? It's currently loading the hidden div and sending results as soon as I click submit. What am I doing wrong?
<?php
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$name = stripslashes($name);
$address = stripslashes($address);
$city = stripslashes($city);
$state = stripslashes($state);
$phone = stripslashes($phone);
$email = stripslashes($email);
$to = 'myemail#thisismywebsite.com ' . ', ';
$to .= $Email;
$from = "$Email ";
$subject = 'Look and Learn: Applicant';
$message = <<<EOF
<html>
<body bgcolor="#FFFFFF">
<b>Look and Learn: Applicant</b><br /><br />
<b>Name:</b> $name<br />
<b>Address:</b> $address / $city, $state<br />
<b>Phone:</b> $phone<br />
<b>Email:</b> $email<br />
</body>
</html>
EOF;
//end of message
// Additional headers
$headers .= 'From: Razor Chic of Atlanta <info#thebrlab.com>' . "\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
mail($to, $subject, $message, $headers);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Sign Up</title>
<link rel="stylesheet" href="css/sign-up.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function() {
$('#thankyou').show();
$("#hidden1").html($("#thankyou").html());
});
});
</script>
<script>
$('#myContact').submit(function () {
$.each($('#myContact .notShouldBeBlank'), function()
{
if($(this).val() == ''){
$(this).after('<span class="error">This field is required.</span>');
}
});
// Other groups validated here
}
</script>
</head>
<body style="overflow: hidden; overflow-x: hidden; overflow-y: hidden;">
<div id="wrap">
<div id="hidden1"></div>
<div style="font-size: 18px; font-weight: bold; font-family: Verdana, Geneva, sans-serif;">
Sign-Up: Look And Learn Class
</div>
<br>
<form id="form" action="" name="myContact" onSubmit="return validateForm()" method="post" enctype="multipart/form-data">
<div>
<label>
<span>Name: *</span><br>
<input name="name" type="text" size="64" placeholder="Name">
</label>
</div>
<div>
<table width="100%" >
<tr>
<td width="230">
<label>
<span>Address: *</span><br>
<input placeholder="Address" size="100" type="text" name="address" maxlength="100">
</label>
</td>
<td width="160">
<label>
<span>City *</span><br>
<input placeholder="City" name="city" type="text" id="city" maxlength="100" />
</label>
</td>
<td width="189">
<label>
<span>State *</span><br>
<input placeholder="State" name="city" type="text" id="city" maxlength="3" />
</label>
</td>
</tr>
</table>
</div>
<div>
<label>
<span>Phone: *</span><br>
<input placeholder="Phone" size="64" type="text" name="phone">
</label>
</div>
<div>
<label>
<span>Email: *</span><br>
<input placeholder="Email address" size="64" type="email" name="email">
</label>
</div>
<div>
<button name="submit" type="submit" id="submit">S I G N U P</button>
</div>
</form>
<p>Note: * Fields are required</p>
</div>
<!---- THANK YOU---->
<?php
if($sent){
echo '<div id="thankyou" style="display:block;">';
}
else{
echo '<div id="thankyou" style="display:none;">';
}
?>
<!---- PAY BEGINS ---->
<div id="paynow1-wrapper">
<div id="paynow1">
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="business" value="Razorchicofatlanta#gmail.com">
<input type="hidden" name="item_name" value="Look and Learn: Deposit">
<input type="hidden" name="amount" value="100.00">
<input type="hidden" name="return" value="http://thebrlab.com/razor-chic-of-atlanta/thank-you.html">
<input type="hidden" name="undefined_quantity" value="1">
<input style="background: none" onMouseOver="this.src='images/pay-now-up.png'" onMouseOut="this.src='images/pay-now-down.png'" type="image" src="images/pay-now-down.png" height="41" width="141" border="0" alt="Pay Now" class="button">
</form>
</div>
</div>
<!---- PAY ENDS ---->
<!---- PAY BEGINS ---->
<div id="paynow2-wrapper">
<div id="paynow2">
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="business" value="Razorchicofatlanta#gmail.com">
<input type="hidden" name="item_name" value="Look and Learn: Balance">
<input type="hidden" name="amount" value="99.00">
<input type="hidden" name="return" value="http://thebrlab.com/razor-chic-of-atlanta/thank-you.html">
<input type="hidden" name="undefined_quantity" value="1">
<input style="background: none" onMouseOver="this.src='images/pay-now-up.png'" onMouseOut="this.src='images/pay-now-down.png'" type="image" src="images/pay-now-down.png" height="41" width="141" border="0" alt="Pay Now" class="button">
</form>
</div>
</div>
<!---- PAY ENDS ---->
<!---- PAY BEGINS ---->
<div id="paynow3-wrapper">
<div id="paynow3">
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="business" value="Razorchicofatlanta#gmail.com">
<input type="hidden" name="item_name" value="Look and Learn: Full Payment">
<input type="hidden" name="amount" value="199.00">
<input type="hidden" name="return" value="http://thebrlab.com/razor-chic-of-atlanta/thank-you.html">
<input type="hidden" name="undefined_quantity" value="1">
<input style="background: none" onMouseOver="this.src='images/pay-now-up.png'" onMouseOut="this.src='images/pay-now-down.png'" type="image" src="images/pay-now-down.png" height="41" width="141" border="0" alt="Pay Now" class="button">
</form>
</div>
</div>
<!---- PAY ENDS ---->
<img src="images/thank-you/look-and-learn1.png" />
</div>
<!---- THANK YOU---->
</body>
</html>
Your form ID is "form", not "myContact". Change form id to myContact.
Then you have 2 "onsubmit" events, one in jQuery and the other inline (onSubmit). Try to merge them into one to avoid unexpected behaviors.
Also, make sure you return false on your submit event function if the validation fails, so the submit is halted.
Update 1
JavaScript:
$('#myContact').submit(function () {
$.each($('#myContact .notShouldBeBlank'), function()
{
if($(this).val() == ''){
$(this).after('<span class="error">This field is required.</span>');
return false;
}
});
/* Uncomment the line below if you really have a validateForm() function */
// return validateForm()
}
HTML:
<form id="myContact" action="" name="myContact" method="post" enctype="multipart/form-data">
I got a page that contain X forms, in each form there is a number of checkboxes.
How do I make it so when I click on a checkbox (as displayed below), then all checkboxes in that specific form will be selected? (and un-selected if ... un-checked):
I tried the following, but it does not work (name="selectfile[]" must not be changed):
I'm not a JS king :(
function selectAll(){
t=document.forms[0].length;
for(i=1; i<t-1; i++) document.forms[0][i].checked=document.forms[0][0].checked;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type='checkbox' name='checkall' style="margin-right: 15px;" onclick='selectAll(this.form.selectfile[]);'>
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
</form>
<script type="text/javascript">
function selectAll(val) {
var checks = document.getElementsByName("selectfile[]");
for (var i = 0; i < checks.length; i++) {
checks[i].checked = val;
}
}
</script>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type='checkbox' name='checkall' style="margin-right: 15px;" onChange="selectAll(this.checked)">
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
<input type="checkbox" name="selectfile[]" value="<? echo $file_fetch['id']; ?>" style="margin-right: 15px;" />
</form>
You can test the javascript part here: http://jsfiddle.net/protron/2t987/
Edit: Based on your comments about having multiple forms and all the checkboxes with the same name, I updated the code and added an extra parameter to the function indicating the form index, and then used document.forms[formIndex] to get the checks. This seems to be the only easy way for this requirement without using jQuery, although IMHO it's more error-prone this way.
Code here: http://jsfiddle.net/protron/2t987/1/