Radio buttons with as-is/update/delete functionality - php

So I have created a table that has 3 radio buttons. I am having trouble understanding how to get each radio button to perform their named task [function]. Here is a picture of my table:
Can someone explain or give me an example as to how to give functionality to these radio buttons. So that when text is edited and say "update" is selected and the submit button is clicked it would update the database.
Here is my code for this section:
<!DOCTYPE html PUBLIC>
<?php
$connection = mysql_connect('localhost', 'root', 'root');
//Test Connection
if (!$connection)
{
die ("Could not connct to the database: <br />". mysql_error());
}
//select db
mysql_select_db('Assets');
$sql1= "SELECT gid, gstmnt FROM Goals";
$records1 = mysql_query($sql1);
//Adding functions to submit
//$gstmnt_up = $_POST['gstmnt'];
?>
<html>
<head>
<title>List of GOALS/List of SERVICES</title>
</head>
<body>
<table width='800' border='3' cellpadding='2' cellspacing='2'>
<tr>
<h1>List of Goals</h1>
<th>GID</th>
<th width= '200'>Goal Statement</th>
<!--Sumbit column-->
<th width= '85'>
<form id="EditGoalForm" action="EditGoals.php" method="POST">
<input type="submit" name="submit" value="Submit"/>
</form>
</th>
</tr>
<!--This is a paragraph. It is editable. Try to change this text.</p>-->
<?php
while ($gtable = mysql_fetch_assoc($records1)) {
echo "<tr>";
echo "<td>".$gtable['gid']."</td>";
echo "<td><div id=textarea contenteditable>".$gtable['gstmnt']."</div></td>";
echo "<td>
<input type='radio' name=".$gtable['gid']." value='asis' checked> AS-IS </br>
<input type='radio' name=".$gtable['gid']." id='radio_update".$gtable['gid']." value='update' > Update </br>
<input type='radio' name=".$gtable['gid']." value='delete' > Delete
</td>";
echo "</tr>";
}
echo "<td> </td>";
echo "<td>
<input type= 'text name='addGoal' placeholder='ADD NEW GOAL HERE' size='100'>
</td>";
echo "<td>
<input type='checkbox' name='add' value='checked'> Add Goal
</td>";
?>
</table>

Related

search in php based on user id key in

I tried this code but it displays nothing. It should display a table when I key in the user id
this is my full code (all in one page) . Can u check the code for me? How am I suppose to call other user id using the code below?
<head>
<title>action</title>
<body bgcolor="#FFFFFF" text="#000000" link="#333333">
<form name="form1" action="test.php" onSubmit="return validateForm()" method="post">
<p><img src="joho.jpg" width="334" height="203"> <img src="msc.jpg" width="769" height="200">
</p>
<p>
<?php
session_start();
?>
<?php
include 'config1.php';
$userid=$_SESSION['userid'];
$query = "SELECT * FROM login where userid='$userid'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<b>WELCOME {$row['userid']} AS {$row['userrole']} </b>";
}
?>
<kbd>
LOG OUT</font></b> <b> </b> </kbd></p>
<p> </p>
<p><kbd>Search user based on user id : </kbd> </p>
<input type="text" name="search" size = "30">
<input type="submit" name = "submit" value="SEARCH" />
</form>
<p> </p>
<p> </p>
<p>
<?php
include 'config1.php';
if(isset($_POST['submit'])){
$searchTerm = mysql_real_escape_string($_POST['search']);
$query = "SELECT * FROM login WHERE userid LIKE '%searchTerm%'";
$result = mysql_query($query);
echo "<table height = '30%'border='1'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td width='5%'><b>USER ID:</b> {$row['userid']} </td>";
echo "<td width='5%'><b>USER NAME :</b> {$row['username']} </td>";
echo "<td width='5%'><b>USER EMAIL:</b> {$row['useremail']} </td>";
echo "<td width='5%'><b>USER DIVISION:</b> {$row['userdiv']} </td>";
echo "<td width='5%'><b>USER DEPARTMENT:</b> {$row['userdepartment']} </td>";
echo "</tr>";
}
}
echo"</table>";
?>

I want to send the values entered by the user in a text input on PHP, through a form in HTML

I have a table in HTML coded inside PHP, which has 3 editable text boxes for the user to edit and a submit button. My code looks as follows:
<html>
<head>
<title>test</title>
</head>
<body>
<?php
echo "<table cellpadding=10 cellspacing=0 border=1>";
echo "<tr><th><b>First Name</b></th><th><b>Surname</b></th><th><b>Favourite Colour</b></th></tr>";
echo "<tr>";
echo "<td><input type=\"text\" name=\"t1\" size=\"25\" value=\"text 1\" /></td>";
echo "<td><input type=\"text\" name=\"t2\" size=\"25\" value=\"text 2\" /></td>";
echo "<td><input type=\"text\" name=\"t3\" size=\"25\" value=\"text 3\" /></td>";
echo "</tr>";
echo "</table>";
echo "<br /><br />";
?>
<form method=post action=display.php>
<input type="hidden" name="text1" value="">
<input type="hidden" name="text2" value="">
<input type="hidden" name="text3" value="]">
<input type="submit" name="submit" value="send">
</form>
</body>
</html>
Inside my form, I want to set the value="", to the value the user has entered above for each respective box. I'm not sure how to do this.
Use Javascript for that.
<script>
document.getElementsByName("text1").value = document.getElementsByName("t1").value;
document.getElementsByName("text2").value = document.getElementsByName("t2").value;
document.getElementsByName("text3").value = document.getElementsByName("t3").value;
</script>

Why won't my variables pass along each page? Php

I can't get my variable "quiz_name" passed along several pages in order to make a quiz. After the "quiz_name" variable is sent from question_menu.php as a $_POST[] to quiz_created.php for processing the quiz_created.php is suppose to send the variable back to question_menu.php. What am I doing wrong. To be honest I think I'm approaching it wrong.
Reworded:
quiz_name = "How to score a Basket" which is submitted to question_menu.php as a $_POST['quiz_name']. Then is submitted a to quiz_created.php as a $_POST['quiz_name'] and then submitted back to question_menu.php.
I get an Variable quiz_name undefined error after it is sent back to quesion_menu.
coach_quizzes.php
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Current Quizzes </h1>
<form name="submit_button" action="create-quiz.php">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
submit to -> create_quiz.php
<?php session_start()
?>
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Enter Quiz Name </h1>
<form action="questions_menu(test).php" method="post">
<input type="text" name="quiz_name" maxlength="30" size="30">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
Enter: "How to score a Basket" then submit to question_menu.php
<!DOCTYPE html>
<html>
<head>
<title>Add Question</title>
<link rel="stylesheet" type="text/css" href="css/info_style.css" />
</head>
<body>
<div id="main">
<header>
<div id="welcome">
<h2>Prairie View A&M University</h2>
</div><!--close welcome-->
</header>
<div id="site_content">
<form enctype="multipart/form-data" method="post" action="quiz_created.php">
<table border="0" name"form_table">
<tr>
<td>Quiz Name</td>
<td><?php echo $_POST['quiz_name']?></td>
</tr>
<tr>
<td>Question</td>
<td><textarea name="description" rows="4" cols="50"></textarea></td>
</tr>
<tr>
<td>Option 1</td>
<td><input type="text" name="option1" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 2</td>
<td> <input type="text" name="option2" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 3</td>
<td><input type="text" name="option3" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Answer</td>
<td>
<select name="dropdown">
<option value='option1'>Option 1</option>
<option value='option2'>Option 2</option>
<option value='option3'>Option 3</option>
</select>
</td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="file" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000" /><br>
</td>
</tr>
<tr>
<td colspan="2"><p>
<input type="submit" value="Add Question">
</p></td>
</tr>
</table>
</form>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
//echo "Connected to mysql<br>";
mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
//update when update button pressed
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};//end of if statement
//delete when delete button pressed
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};//end of if statement
$mysql = "SELECT * FROM $table";
$mydata = mysql_query($mysql,$con);
//create table
echo "<table border=1
<tr>
<th>Question ID</th>
<th>Quiz Name</th>
<th>Question Description</th>
<th>Option 1</th>
<th>Option 2</th>
<th>Option 3</th>
<th>Answer</th>
<th>Picture</th>
</tr>";
//insert data into rows
while($records = mysql_fetch_array($mydata)){
echo "<form action=questions_menu(test).php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['quiz_name']." </td>";
echo "<td>"."<textarea name=description rows=1 cols=25>".$records['question_description']."</textarea>"."</td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>";
echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>";
echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>";
echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>";
echo "<td>". $records['image'] ." </td>";
echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>";
//update button
echo "<td>"."<input type=submit name=update value=Update onclick='return confirm(\"Are you sure you want to update question?\")'>"." </td>";
//delete button
echo "<td>"."<input type=submit name=delete value=Delete onclick='return confirm(\"Are you sure you want to delete question?\")'>"." </td>";
echo "</tr>";
echo "</form>";//end form
} echo "</table>";
mysql_close();
?> <!-- End of php code-->
</div><!--close site_content-->
<footer>
Home | Photos | Videos | Schedule | Contact<br/><br/>
</footer>
</div><!--close main-->
</body>
</html>
after the information is added submit value= "Add Question" information is sent to quiz_created.php:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";
$db = mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
$mysql = "INSERT INTO $table(question_description, quiz_name, option_a, option_b, option_c, answer) VALUES('$_POST[description]','$_POST[quiz_name]','$_POST[option1]','$_POST[option2]','$_POST[option3]','$_POST[dropdown]')";
if(!mysql_query($mysql))
echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
//die("Disconnected");
$quiz=$_POST['quiz_name'];
//Upload images
if($_POST)
{
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$image_loc=addslashes (getcwd()."/uploaded"."/".$_FILES["file"]["name"]);
$query_image = "insert into $table (image, img_location) values ('".$_FILES['file']['name']."', '$image_loc')";
if(mysql_query($query_image))
{
echo "Stored in: " . "uploaded/" . $_FILES["file"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}
}
}
//end of image upload
mysql_close();
?>
<html>
<title>User Added</title>
<body>
<h2>
Question has been added!
</h2>
</body>
<form action="questions_menu(test).php" method="post">
<input type="submit" value="Add Another Question">
<input type="hidden" name="quiz_name" value='<?php echo $quiz;?>'>
</form>
</html>
then finally sent back to question_menu.php but I get an Variable quiz_name undefined error.
I hope I can get help. Thank You.
I see many problems, but one is that you're not escaping your php inserts properly.
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
should be:
echo '<td><input type="text" name="questionID" size="5" value="'.$records['question_id'].'" /></td>';
The way you have it now, you are using the quotes for the echo and there are none for the value="". This is seen all throughout your code. Not sure where you are getting your $records array either, but that's for you to figure out.
These lines are problematic too.
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
One major problem is you're trying to set raw POST data into a database. That is a HUGE security flaw. Other problems, like the rest of your php, have to do with quotes and double quotes, semicolons, etc... Read up on syntax. Your whole script is basically wrong.

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

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.

Need to get values in textbox from database on button click in PHP

I need to get values of database in text boxes on button click using PHP and Javascript. For instance, I get values in an HTML table from a database table. I need to get the respective values in the text boxes when the user clicks on the add0 button.
Here is my code:
<form method="post" action="">
<input type="text" name="tb1" />
<input type="text" name="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td width='130px'>$row[Name]</td>";
echo "<td width='230px'><a href = '$row[Link]'>$row[Link]</a></td>";
echo "<td width='130px'>$add_to_textbox</td>";
echo "</tr>";
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
I need further code on button click.
imho you can use Inline edit using Ajax in Jquery
Here is it's demo
It will let you edit your displayed contents in the table itself..
Update:
<form method="post" action="">
<input type="text" name="tb1" id="tb1" />
<input type="text" name="tb2" id ="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
$rowID=1;
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td width='130px' id='name".$rowID."'>$row[Name]</td>";
echo "<td width='230px' id='link".$rowID."'><a href = '$row[Link]'>$row[Link]</a></td>";
echo "<td width='130px' onclick='txtValDisp($rowID);'>$add_to_textbox</td>";
echo "</tr>";
$rowID++;
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
<script type="text/javascript">
function txtValDisp(rowID){
var linkVal = document.getElementById('link'+rowID+'').innerHTML.replace(/<\/?[^>]+(>|$)/g, "\n");
document.getElementById("tb1").value = document.getElementById('name'+rowID+'').innerHTML;
document.getElementById("tb2").value = linkVal;
}
</script>
Recreate your form with default values taken from the database.
<form method="post" action="">
<input type="text" name="tb1" />
<input type="text" name="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td><input name ='INSERT_HERE' type=text value='"$row[Name]"'></td>";
echo "</tr>";
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
You just need to change the name of the object based on whatever counter of something...

Categories