How can I put the name of checkbox in correct way then it can run with the javascript
<?php $i=1; ?>
<?php do { $i++; ?>
<tr>
<td><input name="<?php echo 'checkbox[$i]' ; ?> " type="checkbox" id="<?php echo $row_RsActivitynoteMem['id']; ?>" value="<?php echo $row_RsActivitynoteMem['id']; ?>" />
<label for="checkbox"></label></td>
<td><?php echo $row_RsActivitynoteMem['Sname']; ?> <?php echo $row_RsActivitynoteMem['Ssurname']; ?></td>
<td width="20"><?php echo $row_RsActivitynoteMem['idactivity']; ?></td>
<td><?php echo $row_RsActivitynoteMem['kname']; ?></td>
<td> </td>
</tr>
<?php } while ($row_RsActivitynoteMem = mysql_fetch_assoc($RsActivitynoteMem)); ?>
<tr>
<td> </td>
<td> </td>
<td width="20"> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.form1.checkbox)">
<input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll(document.form1.checkbox)">
<br>
</form>
</body>
</html>
<?php
mysql_free_result($RsActivitynoteMem);
?>
How can I make CheckAll at the bottom work and handle the check box variable when sending to another page?
This javascript worked:
function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}
function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
// End --
You should probably change your 5th code lines "name"-attribute, because PHP doesn't parse variables in single quotes.
It should be
<?php echo "checkbox[$i]"; ?>
or even better
<?php echo sprintf( 'checkbox[%s]', $i ); ?>
Otherwise the name in your HTML-File will in fact be like <input name="checkbox[$i]" type="checkbox" ... />
Related
//Deleting is working. However, I can't delete the specified row in the table. It always deletes the last row. I hope you could help me. Thank you! This is my code for displaying data from database:
<form action="deleteCart.php" method = "post" role="form">
<?php
while ($row = mysqli_fetch_array($result2)) {
?>
<tr style="text-align: center;">
<td> <img src="images/<?php echo $row["ImageProduct1"]; ?>"/>
<td><?php echo $row['NameProduct1']; ?> </td>
<td>#<?php echo $row['OrderID']; ?></td>
<td><?php echo $row['OrderQuantity']; ?></td>
<td><input type="submit" name="cancelOrder" value = "Cancel" ></td>
<td><input type="hidden" name="hiddenID" value="<?php echo $row['OrderID']; ?>"></td>
</tr>
<?php
}
?>
</form>
//This is my code for deleting:
if(isset($_POST['cancelOrder'])){
orderID = $_POST['hiddenID'];
mysqli_query($con, "DELETE FROM OrderTable WHERE OrderID=$_POST[hiddenID];");
header('location: deleteCart.php');
}
Delete only the last record because you submitting form whole table record. you should try this code. it will work fine.
this will submit separate record.
<?php
while ($row = mysqli_fetch_array($result2)) {
?>
<form action="deleteCart.php" method = "post" role="form">
<tr style="text-align: center;">
<td><img src="images/<?php echo $row["ImageProduct1"]; ?>"/>
<td><?php echo $row['NameProduct1']; ?> </td>
<td>#<?php echo $row['OrderID']; ?></td>
<td><?php echo $row['OrderQuantity']; ?></td>
<td>
<input type="hidden" name="hiddenID" value="<?php echo $row['OrderID']; ?>">
<input type="submit" name="cancelOrder" value = "Cancel" >
</td>
</tr>
</form>
<?php
}
?>
In a quiz app, I am taking user answer using a form. I am retrieving correct answer from database table. I want to compare the correct answer with the user's answer and count how many answer was right and how many answer was wrong.
Here is my form:
<form id="question" class="" action="quiz_ans.php" method="post">
<table id="quiz-question" align="center" class="row-border compact order-column stripe">
<input class="form-control" type="hidden" name="NumberofQuestions" id="NumberofQuestions" value="<?php echo $NumberofQuestions; ?>">
<thead>
<?php
if($QuizQuestions) {
$i=1;
foreach($QuizQuestions as $row):
?>
<tr>
<th><?php echo $i; ?>. <?php echo $row->Question; ?>
<br>
<?php if(isset($row->Screenshot)) { ?>
<img src="<?php echo htmlspecialchars($row->Screenshot); ?>" alt="test" height="300" width="980">
<?php } ?>
</th>
</tr>
</thead>
<tbody>
<?php if(isset($row->Option1)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="0"><?php echo $row->Option1; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option2)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="1"> <?php echo $row->Option2; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option3)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="2"> <?php echo $row->Option3; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option4)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="3"><?php echo $row->Option4; ?></td>
</tr>
<?php } ?>
<tr>
<td><label for="AnswerReason">Why?</label><input class="form-control" type="text" name="AnswerReason[]" id="AnswerReason" value=""></td>
</tr>
<?php if(isset($row->Id)) { ?>
<tr>
<td><input class="form-control" type="hidden" name="QuestionId[]" id="QuestionId" value="<?php echo $row->Id; ?>"></td>
</tr>
<?php } ?>
</tbody>
<?php
$i++;
endforeach;
}
?>
</table>
<br>
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</form>
I am getting the user answer from the form submit:
$NumberofQuestions = $_POST['NumberofQuestions'];
$ans = implode("", $_POST['AnswerId']);
I am retreiving the correct answer from the database table:
try {
$sql = "CALL spQuizAnswers(:quiz_num)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':quiz_num', $quiz_num, PDO::PARAM_STR, 50);
$stmt->execute();
$QuizAns=$stmt->fetchAll();
$stmt->closeCursor();
} catch (PDOException $e) {
die("Error occurred:" . $e->getMessage());
}
I am comparing the user's answer and the correct answer:
for ($i=0; $i<$NumberofQuestions; $i++) {
if($QuizAns) {
foreach($QuizAns as $row):
if($row->CorrectAns == $ans[$i]){
$right++;
} elseif($ans[$i] == 4){
$not_answered++;
} else {
$wrong++;
}
endforeach;
}
}
$CorrectAnswer = $right;
$WrongAnswer = $wrong;
$NotAnswered = $not_answered;
$TotalQuestion = $right+$wrong+$not_answered;
It does not give correct calculation. For 5 questions it gives $TotalQuestion=25.
How can I achieve the correct calculation? Any help would be much appreciated.
I have a variable putting out the coordinates of a address. I am printing the coordinates before the name to test at the moment. On odd number loops (it is in a foreach loop) it works fine, putting the variable in the data-latLng attribute. While on Even number loops it gives out different values - not coordinates. values like: 2 and ..
Here is what I mean:
An odd numbered loop would print this out:
Meanwhile on an even loop number, the data-latLng attribute puts out different values:
Here is the code:
$area_lat_long = isset($area_lat_long[$mapCounter])?$area_lat_long[$mapCounter]:"-26.2041028, 28.047305100000017";
echo $area_lat_long;
echo '<strong>area: '. $streetAdd[$count] .' <a class="glyphicon glyphicon-new-window" type="button" data-toggle="modal" data-target="#mapModal" data-latLng="'. $area_lat_long .'" style="cursor:pointer;"></a><br>';
$mapCounter++;
The PHP code runs above the table code, the code above gives the line of the coordinates and then the area.
As you can see, the $area_lat_long gives coordinates before every area, but when using the EXACT same variable for the data-latLng it changes on even loops?
Edit
To the guys who wanted the whole loop in the comments:
foreach ($streetAdd as $key){
print_r($area_lat_long);
//LAT LONG
$area_lat_long = isset($area_lat_long[$count])?$area_lat_long[$count]:"-26.2041028, 28.047305100000017";
echo $area_lat_long;
echo '<strong>area: '. $streetAdd[$count] .' <a class="glyphicon glyphicon-new-window" type="button" data-toggle="modal" data-target="#mapModal" data-latLng="'. $area_lat_long .'" style="cursor:pointer;"></a><br>';
$mapCounter++;?>
<input type="hidden" id="street_address" name="street_address[<?php echo $count; ?>]" value="<?php echo $streetAdd[$count];?>">
<table class="table table-striped">
<thead>
<tr>
<th>Media Type</th>
<th>Quantity Required</th>
<th>Average Asset Price</th>
<th><!-- Remaining Total --></th>
<th>More Options</th>
</tr>
</thead>
<tbody class="assetCounter">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$j = 0;
$total_used = 0;
$total_bal = isset($budget)?$budget:0;
$qty = 1;
$i = 0;
foreach ($my_categories as $key) { //loop thru chosen media types
foreach ((array)$key as $data) {
// print_r($data);
//check valid description
$j++;
$data_description = isset($data->mec_description)?$data->mec_description:'';
$latitude_longitude = explode(",",$area_lat_long); //print_r($latitude_longitude);
$latitude = $latitude_longitude[0];
$longitude = isset($latitude_longitude[1])?$latitude_longitude[1]:$latitude;
//pricing
$min_price = isset($data->asg_min_price)?$data->asg_min_price:0;
$max_price = isset($data->asg_max_price)?$data->asg_max_price:0;
$average_p = ($min_price + $max_price)/2;
$total_used += $average_p;
$total_bal -= $average_p;
if($total_bal < 0){
$total_bal = 0;
}
if($average_p == 0){
$title = "Pricing information not yet available from Media Owners";
} else {
$title = "NOTE: These are just estimates/guidelines, latest pricing information will be received from Media Owners quotations";
}
?>
<tr class="asset_<? echo $counterForAsset; ?>">
<td><?php
echo strtoupper($mec_stuff[$i]);
?>
<input type="hidden" id="media_category" name="mec_id[]" value="<?php
foreach($mec_stuff as $ms) {
echo $ms . ',';
}
?>">
<input type="hidden" id="media_category" name="media_category[]" value="<?php echo $data_description; ?>"></input></td>
<td><input type="text" class="form-control q_asset_<? echo $counterForAsset; ?> med_quantity" name="med_quantity[]" id="med_quantity" placeholder="Quantity Required" value="1"/></td>
<td><input type="text" readonly="true" name="avg_total[]" id="asset_<? echo $counterForAsset; ?>" class="form-control avg_asset_<? echo $counterForAsset; ?>" value="<?php echo number_format($total_bal,2); ?>" title="<?php echo $title;?>"/></td>
<!-- <td><input type="text" readonly="true" name="avg_total[]" id="avg_total--><?php //echo $j; ?><!--" class="form-control asset_--><?// echo $i; ?><!--" value="--><?php //echo number_format($total_bal,2); ?><!--" title="--><?php //echo $title;?><!--"/></td>-->
<td><input type="text" readonly="true" name="rem_total[]" id="asset_<? echo $counterForAsset; ?>" class="form-control rem_asset_<? echo $counterForAsset; ?>" value="<?php echo number_format($total_bal,2); ?>"/></td>
<!-- <td><input type="text" readonly="true" name="rem_total[]" id="rem_total--><?php //echo $j; ?><!--" class="form-control --><?// echo $i; ?><!-- asset_--><?// echo $i; ?><!--" value="--><?php //echo number_format($total_bal,2); ?><!--"/></td>-->
<td><?php echo "<a class='js-fire-modal btn btn-info' type='button' data-toggle='modal' data-mecid='$mec_stuff[$i]' href='#' name='size_button' onclick=\"sizeModal2(1, $j, '$latitude','$longitude','$description')\">>>></a>";?></td>
</tr>
<tr>
<td></td>
<td colspan="4" id="<?php echo $j; ?>"></td>
</tr>
<?php $i ++; $counterForAsset++; }
}?>
<tr>
<td> </td>
<td> <input type="hidden" id="hidSubtotal<?php echo $j;?>" value="<?php echo number_format($total_used,2); ?>"></td>
<td> Subtotal</td>
<td> <span id="lblSubtotal<?php echo $j; ?>"><?php echo number_format($total_used,2); ?></span> </td>
</tr>
</tbody>
</table>
<?php $count++;
}
foreach($a as $b) {
$x = isset($x[$c]) ? $x[$c] : "foo7";
echo $x;
}
Let us "run" that piece of code, assuming
$x = array('c' => 'lat,lng');
before the loop.
So loop #1 prints 'lat,lng'.
Loop #2 checks if isset('lat,lng'[$c]), evaluates to false, so $x will be 'foo'.
'foo' will be printed.
Loop #3 does the same as #2, but checks if isset('foo'[$c]), again evaluating to false.
Again 'foo' will be printed.
That's the reason.
I don't know what else you are doing outside that loop and most of the code seems to do nothing or nothing intended really.
I can't do the live search table thing. Can someone help me please?
Here is my code. I want to show only the data I've search.........................................................................................................................................................................................................................................................................
<?php
//include the connection file
include "conn.php";
$sql = "SELECT * FROM tblreservation";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE Name = '{search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST" action="trys.php" align="center">
Search: <input type="text" name="search_box" value="" />
<input type="submit" name="search" value="Search the table...">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
<td>Packages</td>
<td><select name="Packages" class="fieldsize">
<option value="">select package</option>
<option value="budget" <?php if($valid_Packages=='budget') echo "selected='selected'";?>>Budget</option>
<option value="standard" <?php if($valid_Packages=='standard') echo "selected='selected'";?>>Standard</option>
<option value="super" <?php if($valid_Packages=='super') echo "selected='selected'";?>>Super</option>
<option value="mega" <?php if($valid_Packages=='mega') echo "selected='selected'";?>>Mega</option>
</select>
<span class="err"><?php echo $error["Packages"];?></span></td>
</tr>
<td>Contactno</td>
<td>Gender</td>
<td><input type="radio" name="gender" value="male" <?php if($valid_gender=='male') echo "checked='checked'";?> />
Male
<input type="radio" name="gender" value="female" <?php if($valid_gender=='female') echo "checked='checked'";?>/>
Female <span class="err"><?php echo $error["gender"];?></span></td>
<td>file</td>
<td><input type="file" name="file" value="upload" />
<span class="err"><?php echo $error["file"];?></span></td>
<td>Address</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['Name']; ?> </td>
<td><?php echo $row['Email']; ?> </td>
<td><?php echo $row['Packages']; ?> </td>
<td><?php echo $row['Contactno']; ?> </td>
<td><?php echo $row['Gender']; ?> </td>
<td><?php echo $row['file']; ?> </td>
<td><?php echo $row['Address']; ?> </td>
</tr>
<?php } ?>
</table>
You are missing a $ and a space in this line:
$sql .= "WHERE Name = '{search_term}'";
The correct line should be as follows:
$sql .= " WHERE Name = '{$search_term}' ";
The SQL statement you are currently generating is exactly this:
SELECT * FROM tblreservationWHERE Name = '{search_term}'
Additionally, I would recommend checking for the existence of $_POST['search_box'] rather than $_POST['search'] in your if-statement and that it actually has a value before appending it as this is what you actually want to use in your query:
if (isset($_POST['search_box']) && $_POST['search_box']) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE Name = '{$search_term}' ";
}
I am having problem in this situation, i searched alot and don't know what should i do with it....
I have got like 4 checkboxes, on each checkbox a certain function is being performed. I have this html code for checkboxes,
<form action="check.php" method="post">
<table border="1" width="200">
<tr>
<td>
<input type="checkbox" name="first[]" value="option1" />
</td>
<td width="500">
<strong>Option 1</strong>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="first[]" value="option2" />
</td>
<td width="500">
<strong>Option 2</strong>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="first[]" value="option3" />
</td>
<td width="500">
<strong>option 3</strong>
</td>
</tr>
</table>
<table border="1" width="200">
<tr>
<td>
<input type="text" name="entered_value"/>
</td>
</tr>
</table>
<table border="1" width="200">
<tr>
<td>
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
I am accessing these checkboxes using this code
<?php
if (isset($_POST['submit']))
{
if(isset($_POST['first']))
{
$get_data_checkboxes = $_POST['first'];
}
else
{
$get_data_checkboxes = "";
}
print_r($get_data_checkboxes);
exit();
}
?>
What i am facing problem is like i need to run a function on each checkbox ticked, e.g. if the person ticks Option 1 then i should be able to run function 1 and so on...
any help will be appreciated :)
If you want to select multiple checkboxes, you can try something like this:
for( $i = 0; $i < count( $_POST['first'] ); $i++ ) {
if( $_POST['first'][$i] == 'option1' ) {
echo "function1";
} else if( $_POST['first'][$i] == 'option2' ) {
echo "function2";
} else if( $_POST['first'][$i] == 'option3' ) {
echo "function3";
}
}
You don't need the line
if (isset($_POST['submit']))
Without that your code works fine, and you can use that to call your functions like this:
<?php
if(isset($_POST['first']))
{
$get_data_checkboxes = $_POST['first'];
foreach($get_data_checkboxes as $value){
//call functions named option1(), option2(), etc..
call_user_func($value);
}
}
else{
$get_data_checkboxes = "";
}
?>