jQuery update doesnt work - php

I'm designing this code and it doesn't work. Can anyone help me?
The jQuery:
$('.cuttingCheckbox').change(function() {
if (this.checked) {
$.post(
'process_class.php',
{
headmark : $($row[HEAD_MARK]).val(),
headmark_id : $($row[ID]).val()
},
function(response){
this.setAttribute("disabled", true), alert(headmark, headmark_id);
}
);
}
});
and the code,
$sql = "SELECT * FROM FABRICATION WHERE FABRICATION.HEAD_MARK = '{$_POST["hm"]}'";
$query = oci_parse($conn, $sql);
$query_exec = oci_execute($query);
while($row = oci_fetch_assoc($query)) {
echo "<table border='1'>";
echo '<table cellspacing = "0"';
echo '<thead>';
echo '<tr><th>Head Mark/ID</th>
<th>Cutting</th>
</tr></thead>';
echo "<tbody>";
echo "<tr><td><b>$row[HEAD_MARK]/$row[ID]</b></td>";
if ($row['CUTTING'] == 'Y') {
//echo "<td><input type='checkbox' id='cuttingCheckbox' name='cuttingCheckbox' checked='checked' disabled='disabled'/></td>";
echo "<td><img src='../images/fabDone.png' width='30' height='30'></td>";
} else {
echo "<td><input type='checkbox' class='cuttingCheckbox' name='cuttingCheckbox'/></td>";
}
echo "</tr>";
echo "</tbody>";
echo "<table cellspacing = '0'";
}
echo "</table>";
And the process_class.php just processing $row[HEAD_MARK] and $row[ID] passed to update the database. I don't know how to pass the $row[HEAD_MARK] and $row[ID] into the jQuery. Please help me

Your problem might be in this jquery statement
{ headmark : $($row[HEAD_MARK]).val(),
headmark_id : $($row[ID]).val()
}
can you please explain which value you are trying to get from HTML dom tree using jquery.
since you have to either use # if field from which you want get value using jquery has id attribute with same name.
so try like below:
{ headmark : $('#<?php echo $row[HEAD_MARK]?>').val(),
headmark_id : $('#<?php echo $row[ID]?>').val()
}
if field is identified by id.
Edit:
change your PHP code line to look like this:
echo "<td><input type='checkbox' data-headmark=".$row['HEAD_MARK']." data-id=".$row['ID']." class='cuttingCheckbox' name='cuttingCheckbox'/></td>";
And:
change your jQuery code line to look like this:
{ headmark : $(this).data('headmark'),
headmark_id : $(this).data('id')
}
Also:
make sure that in process_class.php script update Query should have quote around head_mark if it is varchar type like below:
"UPDATE FABRICATION_QC SET CUTTING = 'Y'
WHERE HEAD_MARK = '".$_POST["headmark"]."' AND ID = ".$_POST["headmark_id"].";"
Now apply this three changes and try again.

Related

php: onClick event - table

How to use onclick in table?
The following code is my basic code.
php code
$con = mysqli_connect($db_host,$db_user,$db_passwd,$db_name);
mysqli_set_charset($con, "utf8");
$result = mysqli_query($con, "select * from Marker_DB");
echo "<table border=’1′> <tr> <th>num</th> </tr>";
$n = 1;
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>". $row['num'] . "</td>";
echo "</tr>";
$n++;
}
echo "</table>";
mysqli_close($con);
I've made a lot of attempts to use onclick. Here's how I tried:
code
echo "<td>". "onclick='displayComment($row['num'])" . "</td>";
echo "<td onClick="'www.google.com/'">". $row['num'] . "</td>";
How can I use onclick in php?
Give your table some id,
echo "<table border='1′ id='table-id'> <tr> <th>num</th> </tr>";
Then your td will be as it is,
echo "<td>". $row['num'] . "</td>";
In js just write this code and check,
$("#table-id").on("click", "td", function() {
alert($(this).text()); // see if you get the value of $row['num']
// you can play here as you like
var num = $(this).text();
displayComment(num);
});
function displayComment(num) {
// your code
}
What you need to do is to stop the propagation of the parent event when a child is clicked, it's easy done in jQuery, but naively you need to do a little more work:
function displayComment(num){
//Do your stuffs with num
console.log('td clicked');
};
Note, for Firefox you need to pass a event parameter:
<td onclick="displayComment($row['num'])">Column 3</td>

Create hyperlink on table result and fill editable form in other page

I'm having problems to find how to create an hyperlink in a table column result and then, on click, open another page with all fields (textboxes) filled. Imagine when a click an ID, i do a select * from table where column_id = ID... Is there a way to do it?
Thanks.
Best regards
I'm not completely sure what you are asking, but this may help you a bit.
First make a Javascript.
<script type="text/JavaScript">
function selectID() {
var ID = document.getElementById("ID").value;
document.location.href ="yoursite.php?ID="+ID;
}
</script>
Then connect to your database to query the table for a link ID (or more) for example by changing the variable $value.
<?php
//Connect to database
mysql_connect("host", "user", "pass");
mysql_select_db("db_name");
$value = 'something';
$ID = $_GET['ID'];
if (!$ID) {
$ID = 0;
}
if ($ID == 0) {
$query = "SELECT * FROM table WHERE `column_1` = '$value'";
$result = mysql_query($query);
echo "<table>";
while($myrow = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo "ID";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
elseif ($ID > 0) {
$query2 = "SELECT * FROM table WHERE `column_id` = '$ID'";
$result2 = mysql_query($query2);
while($myrow2 = mysql_fetch_array($result2)) {
$value1 = $myrow2['column_1'];
$value2 = $myrow2['column_2'];
}
echo "<form type=\"GET\" action=\"$PHP_SELF\">";
echo "<input type=\"text\" id=\"ID\" name=\"ID\" value=\"$ID\"><br>";
echo "<input type=\"text\" id=\"value1\" name=\"value1\" value=\"$value1\"><br>";
echo "<input type=\"text\" id=\"value2\" name=\"value2\" value=\"$value2\"><br>";
echo "<input type=\"hidden\" id=\"search\" name=\"search\" value=\"searching\">";
echo "<input type=\"submit\" id=\"submitbutton\" name=\"submitbutton\" value=\" Search \">";
echo "</form>";
}
?>

AJAX update database

Sorry for not stated my problem. Actually I want to update my data in database.
But the problem now is even i tried to choose approve or reject the ajax still won't update.
I am new in ajax and try search around net but my code still got problem
here is my php page
<?php
$querysel = "SELECT * FROM tblinternapplication WHERE course_code = '{$course_codeapp}' ORDER BY student_id, 1 DESC " ;
$resultsel = mysql_query($querysel, $connection);
echo "<h2><div class=\"h_title\">Status still in pending</div></h2>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th scope=\"col\">Matric ID</th>";
echo "<th scope=\"col\">Company name</th>";
echo "<th scope=\"col\" width = \"200\">Job Scope</th>";
echo "<th scope=\"col\">Status</th>";
echo "<th scope=\"col\">Action</th>";
echo "</tr>";
echo "</thead>";
while($rowsel = mysql_fetch_array($resultsel)){
if($rowsel['status_approval'] == NULL){
$id = $rowsel['id'];
echo "<tr>";
echo "<tr>"."<td class=\"align-center\">".$rowsel['student_id']."</td>";
echo "<td class=\"align-center\">".$rowsel['company_name']."</td>";
echo "<td class=\"align-center\" width = \"200\">".$rowsel['job_scope']."</td>";
echo "<td class=\"align-center\">";
if($rowsel['status_approval'] != NULL){
if( $rowsel['status_approval'] == 0)
{
echo "Reject";
}
else
{
echo "Approve";
}
}
else
{ echo "Pending";
}
echo "</td>";
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus(this.value)\">";
echo "<option value=\"\">Select status:</option>";
echo "<option value=\"1\">Approve</option>";
echo "<option value=\"0\">Reject</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
here is my jscript page
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function getstatus(id, approve)
{
if (approve=="")
{
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","updatestatus.php?id=" + id + "&status=" + approve,true);
xmlhttp.send();
}
</script>
then here is my updatestatus.php
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codeapp = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<?php
$id = $_GET['id'];
$status =$_GET['status'];
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
$result = mysql_query($sql);
?>
I work for few days but problem still cannot solve. Hope someone can help me. I will appreciate your help!
For preapre result for JS in php use json_encode() function. Make your update script somthing like this:
<?php require_once("../includes/session.php");
require_once("sessioncourse.php");
$course_codeapp = $_SESSION['course_code'] ;
confirm_logged_in();
require_once("../includes/connection.php");
require_once("../includes/functions.php");
$id = $_GET['id'];
$status =$_GET['status'];
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
$result = mysql_query($sql);
$json = array();
while ($row = mysql_fetch_assoc($result)) {
$json[] = $row;
}
echo json_encode($json);
IMPORTANT
Dont close php tag or you may add extra space chars
AJAX
In your case using jQuery ajax was good practic. Mkae you code like this:
<script type="text/javascript">
function getstatus(id, approve)
{
$.ajax({
'url': 'updatestatus.php',
'data': {"id": id, "status": approve},
'success': function (response) {
console.log(response);
//TODO: use server response
}
});
}
</script>
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus(this.value)\">";
In that line above, you are passing only one value. At the same time your JS function waits for 2 parameters. Currently, you have id always equal to 0 or 1 and status is always undefined.
Suppose you just need to change that line like:
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus("+ $id +", this.value)\">";
One note:
$sql="UPDATE tblinternapplication set status_approval  = $status WHERE id = $id ";
Except that you should not use mysql_* functions anymore, code above is opened to sql injections. To avoid sql injections with mysql_ extension you should do something like this:
$sql="UPDATE tblinternapplication set status_approval  = ".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);
See docs for mysql_real_escape_string here. Also, read a warning message on that page - it tells you what you should use in replacement for mysql extension
I would suggest to use the developer tools like F12 in IE or chrome or the fiddler to check the request you are sending to the PHP code . In case you are getting any error you can easily see in the response

JS $('form').change(function() only triggers on first form item

Following on from my question yesterday, I now have the code below somewhat successfully working. It allows me to change the first form item and submits it to 'process.php' in the background and turns the field green. However the trigger only works on the first form item, in this case "cstate". It doesn't trigger when "clocation" is changed. If you change clocation and then cstate then both form submit fine so it's simply that the .change function isn't triggering when clocation is changed. I'm not good enough at JS (total JS noob) to know why it isn't working so I'd appreciate any help you can give me.
Thanks!
$sql = "select * from `$table1`";
$result = mysql_query ($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$carid = $row["car_id"];
$carnum = $row["carnum"];
$carlocation = $row["carlocation"];
$carstate = $row["carstate"];
$formname = "#form".$carid;
print '<script type="text/javascript">';
print " var cnum;";
print " cnum = '$formname',";
print "
$('form').change(function()
{
console.log(cnum);
$.ajax({
type: 'post',
url: 'process.php',
data: $(this).serialize(),
success: function() {
}
});
return false;
});
</script>";
echo "<table>";
echo "<tr id='$carid'>";
echo "<td>$carnum</td>";
echo "<td><form action='' method='post' id='form$carid'>";
echo "<select id='popup' name='cstate'>";
echo "<option value='In-Service-Bay'>In Service Bay</option>";
echo "<option value='Awaiting-Service'>Awaiting Service</option>";
echo "<option value='Service-Complete'>Service Complete</option>";
echo "</select></td>";
echo "<select id='popup' name='clocation'>";
echo "<option value='Carpark-1'>Carpark-1</option>";
echo "<option value='Carpark-2'>Carpark-2</option>";
echo "<option value='Carpark-3'>Carpark-3</option>";
echo "</select></td>";
echo "</form></tr>";
}
echo "</table>";
Use .on() and wrap code inside document.ready.
$(document).ready(function(){
$('form').on('submit',function()
{
//code here
});
});
also put this code outside the while loop. Also form does not have change event try using .submit()
Solved it by using a separate form for each input selection. Works brilliantly now.
Thanks to all those that tried to help.
Adam

Get variable from another table in a WHILE loop

<?php
$id = $_SESSION['user_id'] ;
echo "<form method='post' action='#'>";
echo "</select>
<p>Which Hospital Would You Like to Submit To?</p>";
$queryitem = "SELECT * FROM vendor_hospital WHERE vendor_hospital.user_id = '$id' AND vendor_hospital.approval_status = '1'" or die('MYSQL error: ' . mysql_error());
if ($result = mysql_query($queryitem)) {
if ($success = mysql_num_rows($result) > 0) {
echo "<select name='hospital_name'>";
echo "<option>-- Select A Facility --</option>";
while ($row = mysql_fetch_array($result))
echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
echo "</select><br><br>";
} else {
echo "No results found.";
}
} else {
echo "Failed to connect to database.";
}
echo "<input type='submit' value='Submit' name='submit' class='button' /></form>";
?>
For some reason I'm stuck here. I'm just trying to get the manufacturer name to show in my options instead of the manufacturer_id. The manufacturer name is a foreign key in another table so I can't simply call $row[manufacturer_id] in my option tag. What should I do here? My only thought is to run a query inside the option tag for every manufacturer_id listed as a value but I'm sure that is overkill. Can someone point me in the right direction of a more elegant solution than that?
Not really sure what you are trying to do...but try this.
Replace this:
while ($row = mysql_fetch_array($result))
echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
echo "</select><br><br>";
With this:
while ($row = mysql_fetch_array($result)){
echo "<option value='".$row['manufacturer_id']."'>".$row['manufacturer_id']."</option>";
}
echo "</select><br><br>";
Also be sure to double check and make sure that you are pulling from the right database, that it is populated, you are calling the right table names, etc...

Categories