Sending a value to another page - javascript

Need your help again. I am trying to create a payroll system for a friend of mine and I am stuck with this line of code.
<table border=6 cellpadding=10 cellspacing=0>
<tr align=center valign=middle>
<td>Employee ID</td>
<td>13th Month</td>
<td>Date Updated</td>
<td>Options</td>
</tr>
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_payroll");
$payroll_sql = mysql_query("SELECT * FROM tbl_payroll");
while ($payroll = mysql_fetch_array($payroll_sql))
{
?>
<tr align=center valign=middle>
<td><?=$payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?=$payroll['emp_id']?>"></td>
<td><?=$payroll['datesaved']?></td>
<td><a href=generate_payroll.php?id=<?=$payroll['emp_id']?>>Generate Payroll</a>
</td>
</tr>
<?php
}
?>
</table>
Actually, this code is running without errors but I wonder how can I send the value of the checkbox to the other page specified by the anchor tag("generate_payroll.php?id=...")
How will I do this? Any answer related to this question is highly appreciated. Thank you!

You need a form first of all...
<form id='myForm' action='generate_payroll.php' method='POST'>
<input type='hidden' name=id value='<?php echo $payroll['emp_id']?;>'>
<tr align=center valign=middle>
<td><?=$payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?=$payroll['emp_id']?>"></td>
<td><?=$payroll['datesaved']?></td>
<td><a href='#' class='nextPage'>Generate Payroll</a>
</td>
</tr>
</form>
Store you $payroll['emp_id'] in a hidden field.
And wire up some jquery so you dont have to have a button for Generate Payroll/
$('.nextPage').click(function(){
$('#myForm').submit();
});
Then on your generate_payroll.php page, simply retrieve...
$id = $_POST['id'];
$month = $_POST['13thmonth'];
Or whatever you wanna do with the POST results
Of course if you dont care about having a button, you can bypass jquery altogether and just do...
<form id='myForm' action='generate_payroll.php' method='POST'>
<input type='hidden' name=id value='<?php echo $payroll['emp_id']?;>'>
<tr align=center valign=middle>
<td><?=$payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?=$payroll['emp_id']?>"></td>
<td><?=$payroll['datesaved']?></td>
<td><input type='submit' value='Generate Payroll'></td>
</tr>
</form>

for assigning value to id you are using ?id=<?=$payroll['emp_id']?> as per me you have to code like this :
?id=<? echo $payroll['emp_id']?>
Try following code and check if URL looks as you want.
<tr align=center valign=middle>
<td><?php echo $payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?php echo $payroll['emp_id']?>"></td>
<td><?php echo $payroll['datesaved']?></td>
<td>Generate Payroll
</td>
</tr>

Related

How to update data from dynamically created table to database in php

I have create a table using php dynamically. But i need to update some of the cell value depending on user input. But i can't figure out how to do that. my codes are given below.
I have found a way to make names an array is to use [] in names attribute from stackoverflow. but it didn't work for me.If i echo $_POST['std_name'] in while loop or outside of loop only the last row of student name is counted. Please Someone help. I am stuck here for 3 days.
<form action="" method="post">
<table>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Student Father Name</th>
<th>Student Mother Name</th>
<th>Student Parents Mobile Number</th>
<th>Student Mobile Number</th>
<th>Student Batch Name</th>
<th>Student College Name</th>
<th>Student Gender</th>
<th>Student Picture</th>
<th>Student Admit Date</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
</tr>
<tr>
<?php
$host="localhost";
$username="root";
$password="";
$db="paragon_first_year";
$qry="select * from `student_info`";
$con=mysqli_connect($host,$username,$password);
mysqli_select_db($con,$db);
$res=mysqli_query($con,$qry);
if(mysqli_num_rows($res)>0){
while($dt=mysqli_fetch_array($res)){?>
<td><input type="text" name="std_id[]" value="<?php echo $dt['std_id']?>"></td>
<td><input type="text" name="std_name[]" value="<?php echo $dt['std_name']?>"></td>
<td><input type="text" name="std_father_name[]" value="<?php echo $dt['std_father_name']?>"></td>
<td><input type="text" name="std_mother_name[]" value="<?php echo $dt['std_mother_name']?>"></td>
<td><input type="text" name="std_parent_mob[]" value="<?php echo $dt['std_parents_mob_no']?>"></td>
<td><input type="text" name="std_mob[]" value="<?php echo $dt['std_mob_no']?>"></td>
<td><?php echo $dt['std_batch_name'] ?></td>
<td><?php echo $dt['std_college_name'] ?></td>
<td><?php echo $dt['std_gender'] ?></td>
<td><img style="height:100px;width:100px;" src="uploadImage/<?php echo $dt['std_name']."_".$dt['std_pic'] ?>" alt=""></td>
<td><?php echo $dt['std_admit_date'] ?></td>
<td><input type="text" name="jan[]" value="<?php echo $dt['january']?>"></td>
<td><input type="text" name="feb[]" value="<?php echo $dt['february']?>"></td>
<td><input type="text" name="mar[]" value="<?php echo $dt['march']?>"></td>
<td><input type="text" name="apr[]" value="<?php echo $dt['april']?>"></td>
<td><input type="text" name="may[]" value="<?php echo $dt['may']?>"></td>
<td><input type="text" name="jun[]" value="<?php echo $dt['june']?>"></td>
<td><input type="text" name="jul[]" value="<?php echo $dt['july']?>"></td>
<td><input type="text" name="aug[]" value="<?php echo $dt['august']?>"></td>
<td><input type="text" name="sep[]" value="<?php echo $dt['september']?>"></td>
<td><input type="text" name="oct[]" value="<?php echo $dt['october']?>"></td>
<td><input type="text" name="nov[]" value="<?php echo $dt['november']?>"></td>
<td><input type="text" name="dec[]" value="<?php echo $dt['december']?>"></td>
</tr><?php
}
}
?>
</table>
<center>
<input type="submit" name="submit" value="Update Data">
</center>
</form>
</body>
my table is showing nicely
hope someone help. Thanks in advance.
You can use array keys to pass information. In this example, the first key is the database primary key, and the second key is the variable name.
The resulting array would look something like this:
$row[123][std_name] = 'joe';
$row[123][std_father_name] = 'bob';
... etc
$row[124][std_name] = 'sally';
$row[124][std_father_name] = 'john';
...etc
Then, as you iterate through the rows, you get the key (123) to use to update the database, as in where std_id=123.
Note: I have used prepared statements to show updating the database. This is essential. Never put variables in a query using concatenation ( i.e., "select * from table where id='$id'")
Lastly, note the structure. Start with PHP, no print or echo statements. This allows you to work with user input and then redirect. First initialize, then work with user input, then do page logic, and lastly get out of php and output the view (html)
This example is not copy and paste; it has things that need to be filled in, and probably debugged. It is merely intended to show the big picture.
<?php
// initialize
$host="localhost";
$username="root";
$password="";
$db="paragon_first_year";
$con=mysqli_connect($host,$username,$password);
// a simple wrapper to make the prepared query more manageable
// see https://phpdelusions.net/mysqli/mysqli_connect#helper
function prepared_query($mysqli, $sql, $params, $types = "")
{
$types = $types ?: str_repeat("s", count($params));
$stmt = $mysqli->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
return $stmt;
}
// work with user input
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$dataRows = (array)$_POST['data'];
foreach($dataRows as $id=>$data) {
$sql = "INSERT INTO student_info SET std_name=?, std_father_name=?, " .
// etc .
"WHERE std_id=?";
$params = array(
$data['std_name'],
$data['std_father_name'],
// etc...
$id
);
// this does the prepare, bind, and execute... Yes, I could just do the
// bind and execute here, but the time savings isn't worth the trouble.
$stmt = prepared_query($con, $sql, $params);
}
// always redirect after a POST
header('Location: /path/to/next/page/or/this/page');
die;
}
// no user input; ok to use plain old query.
$qry="select * from `student_info`";
mysqli_select_db($con,$db);
$res=mysqli_query($con,$qry);
?>
<html>
...
<form action="" method="post">
<table>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Student Father Name</th>
<th>Student Mother Name</th>
<th>Student Parents Mobile Number</th>
<th>Student Mobile Number</th>
<th>Student Batch Name</th>
<th>Student College Name</th>
<th>Student Gender</th>
<th>Student Picture</th>
<th>Student Admit Date</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
</tr>
<!-- don't need mysqli_num_rows; the while won't loop if no results -->
<?php while($dt=mysqli_fetch_array($res)): ?>
<tr>
<td><?= $dt['std_id'] ?></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_name']" value="<?= $dt['std_name'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_father_name']" value="<?= $dt['std_father_name'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_mother_name']" value="<?= $dt['std_mother_name'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_parent_mob']" value="<?= $dt['std_parents_mob_no'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_mob']" value="<?= $dt['std_mob_no'] ?>"></td>
<td><?= $dt['std_batch_name'] ?></td>
<td><?= $dt['std_college_name'] ?></td>
<td><?= $dt['std_gender'] ?></td>
<td><img style="height:100px;width:100px;" src="uploadImage/<?= $dt['std_name']."_".$dt['std_pic'] ?>" alt=""></td>
<td><?= $dt['std_admit_date'] ?></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['jan']" value="<?= $dt['january'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['feb']" value="<?= $dt['february'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['mar']" value="<?= $dt['march'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['apr']" value="<?= $dt['april'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['may']" value="<?= $dt['may'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['jun']" value="<?= $dt['june'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['jul']" value="<?= $dt['july'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['aug']" value="<?= $dt['august'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['sep']" value="<?= $dt['september'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['oct']" value="<?= $dt['october'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['nov']" value="<?= $dt['november'] ?>"></td>
<td><input type="text" name="data[<?= $dt['std_id'] ?>]['dec']" value="<?= $dt['december'] ?>"></td>
<?php endwhile; ?>
</tr>
</table>
<center>
<input type="submit" name="submit" value="Update Data">
</center>
</form>
</body>

Send php variable onclick through javascript code

I have a form table, in which when I click on Show button, then a popup window open. Till now everything is good.
But after click on Show button, I also want to send php variable in popup window and want to show values related to that variable. How it can be possible?
My table is below
<table align="center">
<?php
include("connection.php");
$query1=mysql_query("select * from career") or die(mysql_error());
while($row1=mysql_fetch_array($query1))
{
extract($row1);
?>
<tr>
<td>Designation</td>
<td><input type="text" name="des" value="<?php echo $designation; ?>"></td>
</td>
</tr>
<tr>
<td>Number of position</td>
<td><input type="text" name="des" value="<?php echo $no_of_position; ?>"></td>
</td>
</tr>
<tr>
<td>Experience</td>
<td><input type="text" name="des" value="<?php echo $experience; ?>"></td>
</td>
</tr>
<tr>
<td>Qualification</td>
<td><input type="text" name="des" value="<?php echo $qualification; ?>"></td>
</td>
<tr>
<td>Job profile</td>
<td><input type="text" name="des" value="<?php echo $job_profile; ?>"></td>
</tr>
<tr>
<td><?php echo"$id"; ?></td>
<td><button onclick="document.getElementById('id01').style.display='block'" style="width:auto;" id="id1">Show</button>
</td>
</tr>
<tr>
<td></td>
<td> </td>
</tr>
<?php } ?>
</table>
In above code when I click on Show Button, then popup window open, code is below:
<div id="id01" class="modal1">
<form class="modal1-content animate" action="xyz.php">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close modal1">×</span>
</div>
<div class="container">
<div>Content</div>
</div>
</form>
</div>
<script>
// Get the modal1
var modal1 = document.getElementById('id01');
// When the user clicks anywhere outside of the modal1, close it
window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
}
</script>
I want to send id variable, by which I can fetch values from database in popup window.
Thank you.
If I understand correctly you need an ajax call to a php page like this:
modalService.php
if(!isset($_GET['id']){
echo "no request";
exit;
}
$id = $_GET['id'];
//Do your query and echo the modal content
table
<td><?php echo $id; ?></td>
<td>
<button onclick="myFunction('<?php echo $id; ?>')" >Show</button>
</td>
JS
function myFunction(id){
var modal = document.getElementById("id01");
modal!==null && (modal.style.display="block");
var modalServiceUrl = "./modalService.php?id="+id;
//xhr to get modalService
}
EDIT: USING JQUERY
<button data-queryid="<?php echo $id; ?>" class="showModal">Show</button>
jQuery(document).ready(function($){
var modal = document.getElementById("id01");
$("button.showModal").on("click", function(){
modal.fadeIn();
var id = $(this).attr("data-queryid");
var modalServiceUrl = "./modalService.php?id="+id;
modal.load(modalServiceUrl);
});
});

Data Table search is giving error

I'm getting a warning saying requested unknown parameter, and after I press ok the search is not working correctly.
any solution for this
DataTable warning .......... Requested unknown parameter
Please find how i created the table
<table class="dataTable border bordered striped" data-role="datatable" data-auto-width="false" id="brandTable">
<thead>
<tr>
<td style="width: 20px">
</td>
<td class="sortable-column sort-asc">ID</td>
<td class="sortable-column">Brand Name</td>
<td class="sortable-column">Account Manager</td>
<td class="sortable-column">Date Updated</td>
</tr>
</thead>
<tbody>
<?php foreach ($fw->customer($_SESSION['USERID'])->getAllBrands() as $v) { ?>
<tr>
<td>
<label class="input-control checkbox small-check no-margin">
<input type="checkbox">
<span class="check"></span>
</label>
</td>
<td>
<?php echo $v['id']; ?>
</td>
<td>
<a href="updateBrands.html?id=<?php echo $v['id']; ?>">
<?php echo $v['brandName']; ?>
</a>
</td>
<td>
<?php echo $fw->customer($_SESSION['USERID'])->getAccountManagerbyId($v['accountManager_id']); ?></td>
<td>
<?php echo $v['dateCreated']; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
You have an undefined or null value at 3 column of 35 row. You should pass some value at that place .

How to store all column to my database? by Javascript

///some connect to database code here...
<form action="update.php" method="post">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<th width="10">Index</th>
<th>Item</th>
<th>numbers</th>
</tr>
<?php
while($row = mysql_fetch_array($query)){
echo"
<tr>
<td><input type='text' name='indexNumber' value='".$row['indexNumber']."' readonly='readonly' size='4' border='0'></input></td>
<td><textarea name='item' rows='2' readonly='readonly'>".$row['item']."</textarea></td>
<td><textarea name='number' class='txtexample'>".$row['number']."</textarea></td>
</tr>
";}?>
<tr><td colspan="3" align="right"><input type="submit" value="Update"/></td></tr>
</table>
Above is my code, I need update all column of table to my database, if I click submit, just update the last column only.
What wrong in my code? if use javascript, how to solve it?
<?php
include 'connect.php';
$index= $_POST["indexNumber"];
$item1= $_POST["item"];
$number1= $_POST["number"];
$query=mysql_query("UPDATE `file` SET `item`='$item1' WHERE `indexNumber`='$index';");
echo "<script language=javascript>alert('Update Successful');window.location='index.php'</script>";
?>
Above is update.php
You need to use array for this.
<tr>
<td><input type='text' name='indexNumber[]' value='".$row['indexNumber']."' readonly='readonly' size='4' border='0'></input></td>
<td><textarea name='item[]' rows='2' readonly='readonly'>".$row['item']."</textarea></td>
<td><textarea name='number[]' class='txtexample'>".$row['number']."</textarea></td>
</tr>
Please check updated script
///some connect to database code here...
<form action="update.php" method="post">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<th width="10">Index</th>
<th>Item</th>
<th>numbers</th>
</tr>
<?php
while($row = mysql_fetch_array($query)){
echo"
<tr>
<td><input type='text' name='indexNumber[]' value='".$row['indexNumber']."' readonly='readonly' size='4' border='0'></input></td>
<td><textarea name='item[]' rows='2' readonly='readonly'>".$row['item']."</textarea></td>
<td><textarea name='number[]' class='txtexample'>".$row['number']."</textarea></td>
</tr>
";}?>
<tr><td colspan="3" align="right"><input type="submit" value="Update"/></td></tr>
</table>

retrieving radio button value if name is from database(lopped)

i am making a simple quiz
the code below is my code for echoing the questions in the database
i used while loop to output the values in the database
i used the q_question for the name of each radio button, because if i put (sample : name="question" which is fixed, when it loop i am only available to pick 1 radio button even if theres 2 or more questions )
<?php
$tbl_name2="a_quiz"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE q_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr style='overflow:hidden; word-wrap:break-word;'>
<td bgcolor="lightgreen"><strong>Question:</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen" style="max-width: 1000px;"><?php echo $rows['q_question'] ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>A</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="a"><?php echo $rows['a'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>B</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="b"><?php echo $rows['b'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>C</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="c"><?php echo $rows['c'] ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>D</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="d"><?php echo $rows['d'] ?></td>
</tr>
</table></td>
</tr>
<hr>
</table><br>
<?php
}
?>
my problem is i cannot get the values of the radio button
im trying $my_answer=$_POST['$q_question'];(and i think this is so wrong) + submit button
help me get the values of the radio button, using php
but if you got idea using javascript etc.. im happy to see it

Categories