need to delete selected rows through checkbox with a single Delete button - php

okay this is the updated main php page now, ive cleared my previous post to make this clean, FYI...what im posting here are just my test page which is he exact replica of my actual page..just have different php page name...
<link href="jquery-ui-1.10.2.custom/css/dark-hive/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-1.9.1.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js" ></script>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="SMP1_deletefromDB.js"></script>
<script>
$(document).ready(function(){
$("#results").show();
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#RetrieveList").on('click',function() {
var xid = $('#XiD').val();
var date = $('#Date').val();
$.post('resultgenerator_test.php',{xid:xid, date:date}, function(data){
$("#results").html(data);
});
return false;
});
});
//post to delete.php file. In delete.php you can get the id's in $_POST['id'] as multidimensional array. You can handle the delete operation based on that
$(document).ready(function(){
$("#DeletefromDB").on('click',function() {
//get all the checked values from checkboxes
var ids = $('input[name=checkbox]:checked').map(function () {
return this.value;
}).get();
if (ids.length === 0)
return false; //show some error message
//post to delete.php file. In delete.php you can get the id's in $_POST['id'] as multidimensional array. You can handle the delete operation based on that
$.post('deletedata.php',{id : ids}, function(data){
$("#results").html(data);
//handle the message based on success or error
});
return false;
});
});
</script>
</head>
<body class="oneColFixCtrHdr">
<div id="container" style="width:auto">
<div id="header" style="background-color:#7BD12E">
<h1 align="left" style="color:#FFF; font-family: Arial, Helvetica, sans-serif;">PIS Ticket Tracking System</h1>
<!-- end #header --></div>
<div id="mainContent">
<h1 style="font-size:9"></h1>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="Back"></label>
<input type="button" name="Back" id="Back" value="Back To Main" href="#" onclick="return backAway();" />
</p>
</form>
<form id="form2" name="form2" method="post" action="">
<table width="741" border="0" align="center">
<tr>
<th colspan="9" align="center" style="font-size:12px" scope="col">Xid, Name:<span>
<select name="XiD" id="XiD">
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
<option value="EEE">EEE</option>
<option value="FFF">FFF</option>
<option value="" selected="selected"></option>
</select>
</span><span style="font-size:12px">
<label for="date">Date:</label>
<input type="text" name="Date" id="Date" />
</span></th>
</tr>
<tr>
<th colspan="9" scope="col"> </th>
</tr>
<tr>
<th colspan="9" scope="col">
<div align="center">
<input name="action" type="button" id="RetrieveList" value="Retrieve List" />
<input name="action" type="button" id="DeletefromDB" value="Delete from DB" />
<input name="Clear" type="reset" id="Clear" value="Clear" />
</div>
<label for="Clear"></label>
<div align="center"></div></th>
</tr>
</table>
</form>
<div id="results">
</div>
</div>
and here is my 2nd php page that echoes the data into table format: Please read my comments below as I need it to clarify...
jQuery(document).ready(function () {
jQuery("input[name=checkall]").click(function () {
jQuery('input[name=checkall]').prop('checked', this.checked);
jQuery('input[name=checkbox]').prop('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery("input[name=checkbox]").click(function(){
if(jQuery("input[name=checkbox]").length == jQuery("input[name=checkbox]:checked").length) {
jQuery("input[name=checkall]").prop("checked", true);
} else {
jQuery("input[name=checkall]").prop("checked", false);
}
});
});
</script>
<?php
require 'include/DB_Open.php';
$xid = $_POST['xid'];
$date = $_POST['date'];
$sql="SELECT ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name
FROM tbl_main
WHERE employee_id_name = '" . $xid . "' AND resolved_date = '" . $date . "'";
$myData = mysql_query($sql);
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><input id=checkall name=checkall type=checkbox value='' /></th>
<th align='center'>Remedy Ticket No.</th>
<th align='center'>Phone/Incident No.</th>
<th align='center'>Category 2</th>
<th align='center'>Category 3</th>
<th align='center'>Status</th>
<th align='center'>Create Date</th>
<th align='center'>Severity</th>
<th align='center'>Ban Type</th>
<th align='center'>Resolved Date</th>
//*I have added this header so that im fetching the employee_id_name as well but just hiding it so i can delete it from my 3rd php...i used the following but still showing a very small cell on the Xid column...*
<th align='center' style='display:none'>XiD</th>
</tr>";
while($info = mysql_fetch_array($myData))
{
echo "<form action='resultgenerator_test.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . "<input type=checkbox name=checkbox value=" . " </td>";
echo "<td align='center'>" . $info['ars_no'] . "<input type=hidden name=ars_no value=" . $info['ars_no'] . " </td>";
echo "<td align='center'>" . $info['phone_number'] . "<input type=hidden name=phone_number value=" . $info['phone_number'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['category_1'] . "<input type=hidden name=category_1 value=" . $info['category_1'] . "' /> </td>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "<td align='center'>" . $info['status'] . "<input type=hidden name=status value=" . $info['status'] . "' /> </td>";
echo "<td align='center'>" . $info['create_date'] . "<input type=hidden name=create_date value=" . $info['create_date'] . "' /> </td>";
echo "<td align='center'>" . $info['trouble_type_priority'] . "<input type=hidden name=trouble_type_priority value=" . $info['trouble_type_priority'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . $info['ban_type'] . "<input type=hidden name=ban_type value=" . $info['ban_type'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . "<input type=text name=resolved_date value=" . $info['resolved_date'] . " size='8' maxlength='8' /> </td>";
echo "<td align='center'>" . "<input type=hidden name=employee_id_name value=" . $info['employee_id_name'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
include 'include/DB_Close.php';
?>
</body>
</html>
now look at the 3rd php page that ive created as u suggested and explained...please correct if u see any errors on the code:
LATEST UPDATE delete php:
<?php
require 'include/DB_Open.php';
$id = $_POST['id'];
$idtodelete = "'" . implode("','",$id) . "'";
$query = "DELETE FROM tbl_main WHERE ars_no in (" . $idtodelete . ")";
$myData = mysql_query($query);
include 'include/DB_Close.php';
?>
thanks for all the help...:)

Here what you need to do is to handle the update and delete operations with the submit button name. What you need to do is to create two submit buttons with same name but different values.
This is your form :
<form action="" method="post">
<input name="action" type="submit" id="RetrieveList" value="RetrieveList" />
<input name="action" type="submit" id="DeletefromDB" value="DeleteFromDB" />
<input name="Clear" type="reset" id="Clear" value="Clear" />
</form>
I have given same name action for the two submit buttons. Once this form is submitted you can check the $_POST['action'] in the server side.
You can do it like:
if ($_POST['action'] == 'RetrieveList') {
//retreive list functionality
} elseif ($_POST['action'] == 'DeleteFromDB') {
//delete from dB functionality.
}
UPDATE:
You need to change your form like this :
echo "<form action='resultgenerator.php' method='post'>";
echo '<input name="action" type="submit" id="DeletefromDB" value="Delete from DB" />';
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><input id=checkall name=checkall type=checkbox value='' /></th>
<th align='center'>Remedy Ticket No.</th>
<th align='center'>Phone/Incident No.</th>
<th align='center'>Category 2</th>
<th align='center'>Category 3</th>
<th align='center'>Status</th>
<th align='center'>Create Date</th>
<th align='center'>Severity</th>
<th align='center'>Ban Type</th>
<th align='center'>Resolved Date</th>
</tr>";
while($info = mysql_fetch_array($myData)) {
echo"<tr>";
echo "<td align='center'>" . "<input type='checkbox' name='checkbox[]' value='add the id here which needs to be deleted'/></td>";
echo "<td align='center'>" . $info['ars_no'] . "<input type=hidden name=ars_no value=" . $info['ars_no'] . " </td>";
echo "<td align='center'>" . $info['phone_number'] . "<input type=hidden name=phone_number value=" . $info['phone_number'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['category_1'] . "<input type=hidden name=category_1 value=" . $info['category_1'] . "' /> </td>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "<td align='center'>" . $info['status'] . "<input type=hidden name=status value=" . $info['status'] . "' /> </td>";
echo "<td align='center'>" . $info['create_date'] . "<input type=hidden name=create_date value=" . $info['create_date'] . "' /> </td>";
echo "<td align='center'>" . $info['trouble_type_priority'] . "<input type=hidden name=trouble_type_priority value=" . $info['trouble_type_priority'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . $info['ban_type'] . "<input type=hidden name=ban_type value=" . $info['ban_type'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . "<input type=text name=resolved_date value=" . $info['resolved_date'] . " size='8' maxlength='8' /> </td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
Now in your resultgenerator.php file you can check :
if ($_POST['action'] == 'DeleteFromDB') {
$ids_to_be_deleted = isset($_POST['checkbox']) ? $_POST['checkbox'] : array();
//$ids_to_be_deleted will contain all the checked id's from the other page. You can get all those values in the array. Handle the remaining operation for delete here.
}
NEW UPDATE :
So if you are using jQuery to submit your form, then it would be better to change the form submit binding to click function on the button. You can change your form to :
<link href="jquery-ui-1.10.2.custom/css/dark-hive/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.9.1.min.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js" ></script>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="SMP1_deletefromDB.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#RetrieveList").on('click',function() {
var xid = $('#XiD').val();
var date = $('#Date').val();
$.post('resultgenerator.php',{xid:xid, date:date}, function(data){
$("#results").html(data);
});
return false;
});
$("#DeletefromDB").on('click',function() {
//get xid for delete like you do above. create a page delete.php or something and make an ajax call to some page to delete data
return false;
});
});
</script>
</head>
<body class="oneColFixCtrHdr">
<div id="container" style="width:auto">
<div id="header" style="background-color:#7BD12E">
<h1 align="left" style="color:#FFF; font-family: Arial, Helvetica, sans-serif;">PIS Ticket Tracking System</h1>
<!-- end #header --></div>
<div id="mainContent">
<h1 style="font-size:9"></h1>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="Back"></label>
<input type="button" name="Back" id="Back" value="Back To Main" href="#" onclick="return backAway();" />
</p>
</form>
<form id="form2" name="form2" method="post" action="">
<table width="741" border="0" align="center">
<tr>
<th colspan="9" align="center" style="font-size:12px" scope="col">Xid, Name:<span>
<select name="XiD" id="XiD">
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
<option value="EEE">EEE</option>
<option value="FFF">FFF</option>
<option value="" selected="selected"></option>
</select>
</span><span style="font-size:12px">
<label for="date">Date:</label>
<input type="text" name="Date" id="Date" />
</span></th>
</tr>
<tr>
<th colspan="9" scope="col"> </th>
</tr>
<tr>
<th colspan="9" scope="col">
<div align="center">
<input name="action" type="button" id="RetrieveList" value="RetrieveList" />
<input name="action" type="button" id="DeletefromDB" value="DeleteFromDB" />
<input name="Clear" type="reset" id="Clear" value="Clear" />
</div>
<label for="Clear"></label>
<div align="center"></div></th>
</tr>
</table>
<p> </p>
</form>
<div id="results">
</div>
</div>
</body>
</html>
Here What I have done is changed the binding event on form submit and binded on click event of input type buttons. I changed type submit to button.
Now you can check on the click event of each buttons and handle the operations.
UPDATE:
Assuming that you have the retrieved list in your results div. When the user check the checkboxes and click the delete button you can handle it using this script:
$("#DeletefromDB").on('click',function() {
//get all the checked values from checkboxes
var ids = $('input[name=checkbox]:checked').map(function () {
return this.value;
}).get();
if (ids.length === 0)
return false; //show some error message
//post to delete.php file. In delete.php you can get the id's in $_POST['id'] as multidimensional array. You can handle the delete operation based on that
$.post('delete.php',{id : ids}, function(data){
//handle the message based on success or error
});
return false;
});
UPDATE :
This can be your delete.php file
$ids = isset($_POST['id']) ? $_POST['id'] : '';
if (!empty($ids)) {
//implode the id's separated by commaas
$ids_to_be_deleted = implode(',', $ids);
$query = "DELETE FROM your_table WHERE field_to_be_checked IN ($ids_to_be_deleted)";
//now run your query using mysql_query
}
NB: : mysql* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used
Hope this helps you :)

For your first concern, give the same name to both your submit button (e.g: 'action'), and on the server side after submit check the value : if ($_POST['action'] == 'Retrieve List') {} else if ($_POST['action'] == 'Delete from DB')
Second concern, the name of the checkbox is the key :
<input type="checkbox" name="checkbox[]" value="{$info['ars_no']}"/>
Then on server side :
foreach($_POST['checkbox'] as $checkbox) {}

You could use a dropdown list where you can chose if you want to retrieve or delete:
<select name='choseaction'>
<option value="RetrieveList">Retrieve List</option>
<option value="DeletefromDB">Delete from DB</option>
</select>
And then use a single submit button.
<INPUT TYPE ="Submit" Name ="Submit" VALUE ="Submit">
After that you can verify like this:
if ($_POST['choseaction']=='RetrieveList')
{
...
} else if ($_POST['choseaction']=='DeletefromDB')
{
...
}
Hope this helps!
Good day.

Related

Data from table to edit doesnt work mysqli and GET/POST

Im not a PHP pro i started php 3 weeks ago my knowledge is low.
My Problem:
In my outdex.php there is table with content read out of my database.
if i click the icon to edit a column i get to edit.php
But it doesnt shown my editable column.
My Code:
outdex.php
<?php
require('config/config.inc.php');
$sql = "SELECT id, name, Status, Strasse, Telefon, ProblemBeschreibung, Datum FROM probleme WHERE id = 1 OR id > 1";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0) {
// Ausgabe der Daten in einer row
echo '<table class="bordered"><thead>';
echo '<tr>';
echo '<th style="display: none;">ID</th>';
echo '<th>Name</th>';
echo '<th>Status</th>';
echo '<th>Straße</th>';
echo '<th>Telefon</th>';
echo '<th>Problembeschreibung</th>';
echo '<th>Eintragsdatum</th>';
echo '<th width="36"></th>';
echo '</tr></thead>';
echo '<tbody>';
while($adr = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $adr['name'] . "</td>
<td>" . $adr['Status'] . "</td>
<td>" . $adr['Strasse'] . "</td>
<td>" . $adr['Telefon'] . "</td>
<td>" . $adr['ProblemBeschreibung'] . "</td>
<td>" . $adr['Datum'] . "</td>
<td>
<div id=\"test\">
<div id=\"div1\"><form action='edit.php' method='GET'>
<input type='hidden' name='id' value='" .htmlspecialchars($adr['id'], ENT_COMPAT, 'UTF-8') . "'/>
<input type='hidden' name='name' value='".htmlspecialchars($adr['name'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Status' value='".htmlspecialchars($adr['Status'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Strasse' value='".htmlspecialchars($adr['Strasse'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Telefon' value='".htmlspecialchars($adr['Telefon'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='ProblemBeschreibung' value='".htmlspecialchars($adr['ProblemBeschreibung'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Datum' value='".htmlspecialchars($adr['Datum'], ENT_COMPAT, 'UTF-8')."'/>
<input type='image' src='edit.png' height='16' width='16' name='edit' value='Bearbeiten' onClick=\"self.location.href='edit.php?=\">
</form></div>
<div id=\"div2\">
<form action='delete.php' method='POST'>
<input type='hidden' name='id' value='" . htmlspecialchars($adr['id'], ENT_COMPAT, 'UTF-8') . "'/>
<input type='image' src='delete.png' height='16' width='16' name='delete' value='Löschen' onClick=\"self.location.href='delete.php?\">
</form></div>
</div>
</td>
</tr>\n";
}
echo "</tbody></table>";
} else {
echo "0 Einträge gefunden<br>Es konnten keine Einträge in der Datenbank gefunden werden.";
}
mysqli_close($db);
?>
My edit.php
<table class="bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Status</th>
<th>Strasse</th>
<th>Telefon</th>
<th>Problembeschreibung</th>
<th>Eintragsdatum</th>
</tr>
</thead>
<?php
include('config.inc.php');
if(isset($_GET['id'])){
$id = $_GET['id'];
} else {
die('Bitte eine ?id übergeben');
}
$statement = $pdo->prepare("SELECT * FROM probleme WHERE id = ?");
$statement->execute(array($id));
while($row = $statement->fetch()){
echo "<tr>
<form method='post' action='edit1.php'>
<td><input name='id' type='text' value='".$id."'</input></td>
<td><input name='name' type='text' value='" .$row['name']."'</input></td>
<td>
<select name='Status'>
<option>".$row['Status']."</option>
<option>FINISHED</option>
</select>
</td>
<td><input name='Strasse' type='text' value='".$row['Strasse']."'</input></td>
<td><input name='Telefon' type='number' value='".$row['Telefon']."'</input></td>
<td><input name='ProblemBeschreibung' type='text' value='".$row['ProblemBeschreibung']."'</input></td>
<td>".$row['Datum']."</td>
</tr>
</table>";
echo "
<input type='submit' value='Absenden' />
</form>";
}
?>
If I change GET to POST it doesnt work too.
hope you can help me.

Preview data from MySQL through drop down list generated from MySQL

So I already have a drop down list generated from a MySQL db.
The problem I can't solve is how to display a "preview" of a selected option.
It's a part of a game I'm building, and you can go to the "arena".
In the arena, there will be a drop down list of opponents to choose from, all of them have different values attached to them like, name, level, strength and so on.
So if you select "opponent 1" from the list, I want a opponent preview to show, and if I change the drop down to "opponent 2" I want the preview to update.
When the player has decided to fight that opponent, a submit can be made by clicking my "fight" button.
Here is the drop down list I'm using:
EDIT: See my answear below with updated code.
After like 8h of pulling my hair, google and ALOT of reading.. i manage to put this together, and it is working!
HTML code:
<div id='load_npc'>
<table width='720' border='0' align='center'>
<td align='center'>
<form id='form1' name='form1' method='post' action=''>
<fieldset>
<legend><strong>Pick your opponent!</strong></legend>
<p>
<select name='npc_name' id='npc_name'>
<option value='' selected='selected'> Select an opponent.. </option>
<option name='row[name]' value='row[name]'><php_row></php_row> </option>
</select>
<br>
<input type='submit' value='Fight!' \>
</p>
</fieldset>
</form>
</td>
</table>
<div id='results'></div>
</div>
Javascript code, placed in the header:
$(function() { // document.ready
$("#npc_name").on("change", function() {
$.ajax({
url: "system/npc_load.php",
type: "POST",
data: {
npc_name: $(this).val()
},
success: function(data) {
$("#results").html(data);
}
});
});
});
And here is the php file referd to by my script:
<?php
require ("database.php");
$npc_name = $_POST['npc_name'];
$sql="SELECT * FROM npc WHERE name = '" . $npc_name . "'";
$myData = mysqli_query($connection, $sql);
//count if there are any results
$numrow = mysqli_num_rows($myData) or die(mysqli_error($connection));
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo '<fieldset><legend><strong>Information</strong></legend><p>
<table width="auto" height="172" border="0">
<tr><th>Name</th></tr>
<tr><th>Type</th></tr>
<tr><th>Level</th></tr>';
while($info = mysql_fetch_array($myData))
{
echo "<form action='system/npc_load.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['name'] . "<input type=hidden name=name value=" . $info['name'] . " </td>";
echo "<td align='center'>" . $info['type'] . "<input type=hidden name=type value=" . $info['type'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['level'] . "<input type=hidden name=level value=" . $info['level'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</fieldset>";
?>

keep data in form when clicking submit

EDIT: Apologies - all code now pasted below
Apologies for the first time newbie question - I've been looking for an answer here and on google - I get the feeling this is some simple coding i'm messing up.
I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish.
I have a section of the form where this doesn't work however - where you can ask multiple questions and each question can have up to 4 answers.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<title>Test page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="stylenewtest.css" type="text/css" />
<script language="javascript" src="tableadd.js"></script>
</head>
<body>
<?php
$string = isset($_POST['quiz_format']) ? $_POST['quiz_format'] : false;
$quiz_format = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $string);
$stringquiz_100 = isset($_POST['quiz_100']) ? $_POST['quiz_100'] : false;
$quiz_100 = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_100);
$stringquiz_fback = isset($_POST['quiz_fback']) ? $_POST['quiz_fback'] : false;
$quiz_fback = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_fback);
$stringquiz_title = isset($_POST['quiz_title']) ? $_POST['quiz_title'] : false;
$quiz_title = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_title);
$stringquiz_instruct = isset($_POST['quiz_instruct']) ? $_POST['quiz_instruct'] : false;
$quiz_instruct = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\?,\"\'\._#\\/ !&()=\+;:\t\r\n-]", "", $stringquiz_instruct);
$stringquiz_time = isset($_POST['quiz_time']) ? $_POST['quiz_time'] : false;
$quiz_time = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_time);
?>
<div id="content">
<p><span class="error">* required field.</span></p>
<form method="post" action="">
<div class="datagrid">
<table>
<tr><th colspan="2">Please choose the format of the quiz<span class="error">*</span></th></tr>
<tr><td>Hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Hangman") echo "checked";?> value="Hangman"></td></tr>
<tr><td>Gap fill</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Gap fill") echo "checked";?> value="Gap fill"></td></tr>
<tr><td>Multiple choice</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Multiple Choice") echo "checked";?> value="Multiple choice"></td></tr>
<tr><td>Simple Q & A</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Simple Q & A") echo "checked";?> value="Simple Q & A"></td></tr>
<tr><td>Word formation hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Word formation hangman") echo "checked";?> value="Word formation hangman"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Continue repeating the exercise until 100% achieved?<span class="error">*</span></th></tr>
<tr><td>Yes</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="Yes") echo "checked";?> value="Yes"></td></tr>
<tr><td>No</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="No") echo "checked";?> value="No"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Show the answer as correct/incorrect after every question or just at the end?<span class="error">*</span></th></tr>
<tr><td>After every question</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="After every question") echo "checked";?> value="After every question"></td></tr>
<tr><td>Just at the end</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="Just at the end") echo "checked";?> value="Just at the end"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Please enter in the title of the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz title:</td><td><input type="text" name="quiz_title" value="<?php echo $_POST[quiz_title];?>"></td></tr>
</table>
<br />
<table>
<tr><th colspan="2">Please enter in the instructions for the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz instructions:</td><td><textarea name="quiz_instruct" rows="10" cols="40"><?php echo $_POST[quiz_instruct];?></textarea></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<script language="javascript">
window.onload = moreFields;
window.onload = init;
</script>
<table>
<tr><th colspan="5">Please enter in the quiz's questions and answers:<span class="error">*</span></th></tr>
</table>
<div id="readroot" style="display: none">
<input type="button" value="Remove a question field"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a4'][$i] ?>"></td>
</tr>
</table>
</div>
<span id="writeroot"></span><input type="button" id="moreFields" value="Click here to add further question fields" /><br />
</div>
<br />
<div class="datagrid">
<table>
<tr><td><input type="submit" name="Submit1" value="Submit"/></td></tr>
</table>
</div>
</form>
<?php
if ( isset( $_POST['Submit1'] ) ) {
echo "<form method=post action=insert.php>";
echo "<h1>Please confirm the quiz details are correct</h1>";
echo "<div class=datagrid>";
echo "<table>";
if(empty($string)){
echo("<tr><td>Quiz format:</td><td><h3>Please go back and choose a quiz format<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz format:</td><td>" . $quiz_format . "</td></tr>");
echo("<input type=hidden name=quiz_format value='" . $quiz_format . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td>" . $quiz_100 . "</td></tr>");
echo("<input type=hidden name=quiz_100 value='" . $quiz_100 . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td>" . $quiz_fback . "</td></tr>");
echo("<input type=hidden name=quiz_fback value='" . $quiz_fback . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_title)){
echo("<tr><td>Quiz title:</td><td><h3>Please go back and enter in a title for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz title:</td><td>" . $quiz_title . "</td></tr>");
echo("<input type=hidden name=quiz_title value='" . $quiz_title . "' />");
}
if(empty($stringquiz_instruct)){
echo("<tr><td>Quiz instructions:</td><td><h3>Please go back and enter in instructions for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz instructions:</td><td>" . $quiz_instruct . "</td></tr>");
echo("<input type=hidden name=quiz_instruct value='" . $quiz_instruct . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_time)){
echo("<tr><td>Time limit:</td><td>not specified</td></tr>");
}
else
{
echo("<tr><td>Time limit:</td><td>" . $quiz_time . "</td></tr>");
echo("<input type=hidden name=quiz_time value='" . $quiz_time . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td colspan=5>The questions and answers to your quiz:</td></tr>";
$aq = $_POST['q'];
$aa1 = $_POST['a1'];
$aa2 = $_POST['a2'];
$aa3 = $_POST['a3'];
$aa4 = $_POST['a4'];
$N = count($aq);
for($i=1; $i < $N; $i++){
if($aq[$i]==""){
echo("<tr><td>Question:</td><td><h3>Please go back and enter a question</h3></td></tr>");
$showbutton=1;
}
elseif($aa1[$i]==""){
echo("<tr><td>Answer:</td><td><h3>Please go back and enter at least one answer</h3></td></tr>");
$showbutton=1;
}
else{
echo ("<table><tr><td>Question:</td><td colspan=3>" . $aq[$i]. "<input type=hidden name=q[] value='" . $aq[$i] . "'></td></tr>
<tr><td>Answer 1:</td><td>Answer 2:</td><td>Answer 3:</td><td>Answer 4:</td></tr>
<tr><td>" .$aa1[$i] . " <input type=hidden name=a1[] value='" .$aa1[$i] . "'></td>
<td>" .$aa2[$i] . " <input type=hidden name=a2[] value='" .$aa2[$i] . "'></td>
<td>" .$aa3[$i] . " <input type=hidden name=a3[] value='" .$aa3[$i] . "'></td>
<td>" .$aa4[$i] . " <input type=hidden name=a4[] value='" .$aa4[$i] . "'></td> </tr></table>");
}
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td><input type=button value='<< Go Back' onclick='goBack()' /></td></tr>";
if ($showbutton =="1"){
}
else{
echo "<tr><td><input type=submit value=Submit></td></tr>";
echo "</table>";
echo "</form>";
}
}
?>
</div>
</body>
</html>
but every time I submit, the questions and answers carry through but the form fields lose all the data. How can I keep the data in the fields?
Many thanks in advance
You can simply set the value of the input to either $_POST['old_value_name] or $_GET['old_value_name'] depending on which form method you are using. Therefore, your new code would look something like this (assuming that you are looking for the key $i from your question):
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
</tr>
</table>
As far as I've understood your problem you want to keep values in a form after they were sent to server. "I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish"
It's ok. I remember I wanted the same a few months ago. This is why I tried to help.
There is only a problem that after pressing "submit" button form is sent to server and page is reloaded.
In return you get a new page from webserver which you pointed in the form field "action".
I would do it so:
Target form: <br/>
<form id="target" action="index.php" method='post'>
<input name="first_field" type="text" value="<?php if ( isset($_POST['first_field']) )
echo $_POST['first_field']; ?>" /> <br/>
<input name="second_field" type="text" value="<?php if ( isset($_POST['second_field']))
echo $_POST['second_field'];?>" /> <br/>
<p><b>What kind of girls do you like?:</b><Br>
<input type="radio" name="browser" value="brunette" <?php if( $_POST['browser']==="brunette" ) echo "checked"; ?> > brunette<Br>
<input type="radio" name="browser" value="blonde" <?php if( $_POST['browser']==="blonde" ) echo "checked"; ?> > blonde<Br>
<input type="radio" name="browser" value="red" <?php if( $_POST['browser']==="red" ) echo "checked"; ?> > red<Br>
</p>
<input type="submit" />
</form>
Any ideas how it can be changed/improved are encouraged.

How to select/unselect generated checkbox rows with a separate checkbox in header

can someone help me with my code, the code below would generate rows after the query, each row would have its own checkbox...i also have a checkbox in the header...what i want is to check or select all rows using only the checkbox in the header but at the same time i should still be able to select a checkbox per row
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><input name=checkall type=checkbox value='' /></th>
<th align='center'>Remedy Ticket No.</th>
<th align='center'>Phone/Incident No.</th>
<th align='center'>Category 2</th>
<th align='center'>Category 3</th>
<th align='center'>Status</th>
<th align='center'>Create Date</th>
<th align='center'>Severity</th>
<th align='center'>Ban Type</th>
<th align='center'>Resolved Date</th>
</tr>";
while($info = mysql_fetch_array($myData))
{
echo "<form action='getdata.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . "<input type=checkbox name=checkbox value=" . " </td>";
echo "<td align='center'>" . $info['ars_no'] . "<input type=hidden name=ars_no value=" . $info['ars_no'] . " </td>";
echo "<td align='center'>" . $info['phone_number'] . "<input type=hidden name=phone_number value=" . $info['phone_number'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['category_1'] . "<input type=hidden name=category_1 value=" . $info['category_1'] . "' /> </td>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "<td align='center'>" . $info['status'] . "<input type=hidden name=status value=" . $info['status'] . "' /> </td>";
echo "<td align='center'>" . $info['create_date'] . "<input type=hidden name=create_date value=" . $info['create_date'] . "' /> </td>";
echo "<td align='center'>" . $info['trouble_type_priority'] . "<input type=hidden name=trouble_type_priority value=" . $info['trouble_type_priority'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . $info['ban_type'] . "<input type=hidden name=ban_type value=" . $info['ban_type'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . "<input type=text name=resolved_date value=" . $info['resolved_date'] . " size='8' maxlength='8' /> </td>";
Use this
$('input[name=checkall]').click(function(){
var checked = $('input[name=checkall]').is(':checked');
if (checked) {
$('input:checkbox').attr('checked','checked');
} else {
$('input:checkbox').attr('checked','');
}
});
You can use jQuery for this.
Here you have to check for two conditions; when the main checkbox is checked and when all the child checkboxes are checked. So when the main checkbox is checked all the checkboxes should be checked. Similarly when all the child checkboxes are checked the main checkbox should also be checked. You have to take care of these two cases.
There are two methods:
Method 1 (using checkbox name)
jQuery(document).ready(function () {
jQuery("input[name=checkall]").click(function () {
jQuery('input[name=checkall]').attr('checked', this.checked);
jQuery('input[name=checkbox]').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery("input[name=checkbox]").click(function(){
if(jQuery("input[name=checkbox]").length == jQuery("input[name=checkbox]:checked").length) {
jQuery("input[name=checkall]").attr("checked", "checked");
} else {
jQuery("input[name=checkall]").removeAttr("checked");
}
});
});
Method 2 (using checkbox class)
You have to add a class for the main header checkbox and another class to all other checkbox. So your script will be :
jQuery(".selectall").click(function () {
jQuery('.selectall').attr('checked', this.checked);
jQuery('.child_checkbox').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery(".child_checkbox").click(function(){
if(jQuery(".child_checkbox").length == jQuery(".child_checkbox:checked").length) {
jQuery(".selectall").attr("checked", "checked");
} else {
jQuery(".selectall").removeAttr("checked");
}
});
Now add the class selectall to your main checkbox and class child_checkbox to all child checkboxes in the loop. So your HTML will be
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><input name='checkall' type='checkbox' value='' class='selectall'/></th>
<th align='center'>Remedy Ticket No.</th>
<th align='center'>Phone/Incident No.</th>
<th align='center'>Category 2</th>
<th align='center'>Category 3</th>
<th align='center'>Status</th>
<th align='center'>Create Date</th>
<th align='center'>Severity</th>
<th align='center'>Ban Type</th>
<th align='center'>Resolved Date</th>
</tr>";
while($info = mysql_fetch_array($myData))
{
echo "<form action='getdata.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . "<input type='checkbox' name='checkbox' value='' class='child_checkbox'/></td>";
echo "<td align='center'>" . $info['ars_no'] . "<input type=hidden name=ars_no value=" . $info['ars_no'] . " </td>";
echo "<td align='center'>" . $info['phone_number'] . "<input type=hidden name=phone_number value=" . $info['phone_number'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['category_1'] . "<input type=hidden name=category_1 value=" . $info['category_1'] . "' /> </td>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "<td align='center'>" . $info['status'] . "<input type=hidden name=status value=" . $info['status'] . "' /> </td>";
echo "<td align='center'>" . $info['create_date'] . "<input type=hidden name=create_date value=" . $info['create_date'] . "' /> </td>";
echo "<td align='center'>" . $info['trouble_type_priority'] . "<input type=hidden name=trouble_type_priority value=" . $info['trouble_type_priority'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . $info['ban_type'] . "<input type=hidden name=ban_type value=" . $info['ban_type'] . " size='1' maxlength='1' /> </td>";
echo "</tr>";
}
UPDATE :
I think you are using the latest version of jQuery which is jQuery 1.9.1. So there is a change in the method in the script. Instead of attr you have to use prop. The attr method has been depreciated. You can check this link for more info on that jQuery attr-vs-prop
So your script for Method 1 (using checkbox name) should be changed to :
jQuery(document).ready(function () {
jQuery("input[name=checkall]").click(function () {
jQuery('input[name=checkall]').prop('checked', this.checked);
jQuery('input[name=checkbox]').prop('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery("input[name=checkbox]").click(function(){
if(jQuery("input[name=checkbox]").length == jQuery("input[name=checkbox]:checked").length) {
jQuery("input[name=checkall]").prop("checked", true);
} else {
jQuery("input[name=checkall]").prop("checked", false);
}
});
});
If you are using class Method 2 (using checkbox class) then it should be :
jQuery(document).ready(function () {
jQuery(".selectall").click(function () {
jQuery('.selectall').prop('checked', this.checked);
jQuery('.child_checkbox').prop('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery(".child_checkbox").click(function(){
if(jQuery(".child_checkbox").length == jQuery(".child_checkbox:checked").length) {
jQuery(".selectall").prop("checked", true);
} else {
jQuery(".selectall").prop("checked", false);
}
});
});
Also please format your HTML correctly seems to be missing quotes around values of input attributes. Even if it is not strict but is better to follow that.
Hope this helps :)
Try to use jQuery.
Add id to header checkbox:
<input id="checkall" type="checkbox" value='' />
Add classes to other checkboxes:
<input class="chkBox" type="checkbox" value='' />
Add link to jQuery library
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Add JS-snippet to your page
<script language="Javascript">
$(function () {
$('#checkall').on('click', function () {
$('.chkBox').attr('checked', true);
});
});
</script>
You will need to use javascript for this.
The checkbox would look like this:
<input name="CheckAll" type="checkbox" id="CheckAll" value="Y" onClick="ClickCheckAll(this);">
The dynamically created checkboxes should look like this:
<td align="center"><input type="checkbox" name="chkDel[]" id="chkDel<?=$i;?>" value="<?=$info["ars_no"];?>"></td>
And then use this javascript:
<script language="JavaScript">
function ClickCheckAll(vol)
{
var i=1;
for(i=1;i<=document.frmMain.hdnCount.value;i++)
{
if(vol.checked == true)
{
eval("document.frmMain.chkDel"+i+".checked=true");
}
else
{
eval("document.frmMain.chkDel"+i+".checked=false");
}
}
}
</script>
You will also have to change your while loop to this:
<?php
$i = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$i++;
?>

how can i get the value of selected radio button if i click the select button and pass its value in a textbox?

//this is saved in president.php
if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'president'"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border='1' cellpadding='10'>";
// set table headers
echo "<tr><th>Student ID</td><th>Course</th><th>Name</th><th></th></tr>";
while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->studid . "</td>";
echo "<td>" . $row->course . "</td>";
echo "<td>" . $row->fname . " ". $row->mname ." ". $row->lname ."</td>";
echo "<td><input type ='radio' name='radio' value='". $row->studid ."'> </td>";
echo "</tr>";
}
echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";
echo "</table>";
}
else {
echo "No results to display!";
}
}
//this my javascript to read and get the value of my radio
function get_radio_value()
{
for (var i=0; i < document.orderform.radio.length; i++)
{
if (document.orderform.radio[i].checked)
{
var rad_val = document.orderform.radio[i].value;
}
}
}
//here i would show the value of my selected radio as i click the select button on the //textbox where inside my form
" />
//but as i load my page, i got an error
// like this Fatal error: Call to undefined function get_radio_value()
//this is the code for my voting.php
<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab" tabindex="0">President</li>
<li class="TabbedPanelsTab" tabindex="0">Secretary</li>
</ul>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent"><p> </p>
<p><?php require_once('candidate/president.php'); ?></p>
<p> </p></div>
<td width="292" valign="top">
<form method="post" action="voting.php" >
<table width="289" height="76" border="0" cellspacing="1">
<tr>
<td width="131" height="24" ><div align="right">President</div></td>
<td width="151"><div align="left">
<input type="text" name="president" id="radio_value"/>
</div></td>
</tr>
</table>
</form></td>
change this
echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";
In this it is calling php's get_radio_value() function, but the function is written in javascript, so use below code.
echo "<td> <input type='button' name='button' value='select' onclick='get_radio_value()'></td>";
for passing the value of radio button in textbox
<input type="text" name="president" value="WHAT CODE SHOULD I PUT HERE?" id="radio_value"/>
change this line
var rad_val = document.orderform.radio[i].value;
to
return document.getElementById('radio_value').innerHTML = document.orderform.radio[i].value;

Categories