I am a beginner in PHP and Javascript I used a live editable table and I am trying to edit them and then save them to mysql in another page I did like this, but I didn't know how to retrieve where to edit in mysql with the id could some one help please! Am i wrong in the request? it doesn't change in the database like it does in the table in the page. I sent the data like this:
function postedit()
{
var _type = $('#typeid').val();
var _pression = $('#pressionid').val();
var _code = $('input[name="code"]').val();
var _designiation = $('input[name="designiation"]').val();
var _diametre = $('input[name="diametre"]').val();
var _epaisseur = $('input[name="epaisseur"]').val();
var _prix = $('input[name="prix"]').val();
var _etat = $('input[name="etat"]').val();
$.post('update.php', {
posttype: _type,
postpression: _pression,
postcode: _code,
postdesigniation: _designiation,
postdiametre: _diametre,
postepaisseur: _epaisseur,
postprix:_prix,
postetat:_etat
},
function(data){
$('tbody').append(data);
});
}
var _trEdit = null;
$(document).on('click', '.btn-edit',function(){
_trEdit = $(this).closest('tr');
var _code = $(_trEdit).find('td:eq(0)').text();
var _designiation = $(_trEdit).find('td:eq(1)').text();
var _diametre = $(_trEdit).find('td:eq(2)').text();
var _epaisseur = $(_trEdit).find('td:eq(3)').text();
var _prix = $(_trEdit).find('td:eq(4)').text();
var _etat = $(_trEdit).find('td:eq(5)').text();
$('input[name="code"]').val(_code);
$('input[name="designiation"]').val(_designiation);
$('input[name="diametre"]').val(_diametre);
$('input[name="epaisseur"]').val(_epaisseur);
$('input[name="prix"]').val(_prix);
$('input[name="etat"]').val(_etat);
});
$('#btn-update').click(function(){
if(_trEdit){
var _code = $('input[name="code"]').val();
var _designiation = $('input[name="designiation"]').val();
var _diametre = $('input[name="diametre"]').val();
var _epaisseur = $('input[name="epaisseur"]').val();
var _prix = $('input[name="prix"]').val();
var _etat = $('input[name="etat"]').val();
$(_trEdit).find('td:eq(0)').text(_code);
$(_trEdit).find('td:eq(1)').text(_designiation);
$(_trEdit).find('td:eq(2)').text(_diametre);
$(_trEdit).find('td:eq(3)').text(_epaisseur);
$(_trEdit).find('td:eq(4)').text(_prix);
$(_trEdit).find('td:eq(5)').text(_etat);
alert("Ligne Modifier avec succée !");
_trEdit = null;
}
});
My sql request in a file update.php like this I thought that if I get the id from the database and search from it, it seems stupid but I am desperate:
$type_utilisation = $_POST['posttype'];
$pression_tube = $_POST['postpression'];
$code_tube = $_POST['postcode'];
$designiation_tube = $_POST['postdesigniation'];
$diametre_tube = $_POST['postdiametre'];
$epaisseur_tube = $_POST['postepaisseur'];
$prix_tube = $_POST['postprix'];
$etat_tube = $_POST['postetat'];
$id_req = ("select id from $pression_tube where typepehd='".$type_utilisation."' && code='".$code_tube."'");
$id_tube=$id_req;
echo"$id_tube";
if(isset($pression_tube)){
if($pression_tube=="pn4"){
if($type_utilisation=="pehdtelecom" && $pression_tube=="pn4" ){
echo "verifier le type du Tube S.v.p";
} else {
$sql="UPDATE pn4 SET typepehd='".$type_utilisation."',code='".$code_tube."',designiation='".$designiation_tube."',diametre='".$diametre_tube."',epaisseur='".$epaisseur_tube."',prix='".$prix_tube."',etat='".$etat_tube."' where id='".$id_tube."'";
$result = mysqli_query($link, $sql);
if($result){
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
}
thank you, i did solve it by using the id because its unique
$id_tube = $_POST['postid'];
by adding an input to it in type hidden
and make the column invisible by
style="display;none";
Related
This above code is showing data of the selected ward.
$("#sel_ward").change(function () {
var wardid = $(this).val();
$.ajax({
url: 'getWard-details.php',
type: 'post',
data: {ward: wardid},
dataType: 'json',
success: function (response) {
var len = response.length;
// $("#sel_ward").empty();
if (len == 0) {
$("#sel_ward").append('<option value="">No Data Found</option>');
var block_selected = response[i]['block_selected'];
$("#blockid").val(block_selected);
} else {
for (var i = 0; i < len; i++) {
var id = response[i]['id'];
var name = response[i]['name'];
var ward_parshad_name = response[i]['ward_parshad_name'];
var ward_parshad_contact = response[i]['ward_parshad_contact'];
var ward_population = response[i]['ward_population'];
var ward_house_count = response[i]['ward_house_count'];
var ward_voters_count = response[i]['ward_voters_count'];
var ward_polling_both_count = response[i]['ward_polling_both_count'];
var block_selected = response[i]['block_selected'];
var zone_selected = response[i]['zone_selected'];
var sector_selected = response[i]['sector_selected'];
var block_area = response[i]['block_area'];
var block_region = response[i]['block_region'];
$("#ward-name-view").text(name);
$("#ward-parshad-name-view").text(ward_parshad_name);
$("#ward-parshad-contact-view").text(ward_parshad_contact);
$("#ward-population-view").text(ward_population);
$("#ward-house-view").text(ward_house_count);
$("#ward-voters-view").text(ward_voters_count);
$("#ward-polling-booth-view").text(ward_polling_both_count);
}
}
}
});
});
Now i have list of committee whose data m getting through PHP
<?php
// Fetch Committee
$sql_block = "SELECT * FROM committee";
$block_data = mysqli_query($con, $sql_block);
while ($row = mysqli_fetch_assoc($block_data)) {
$committeeid = $row['sno'];
$committee_name = $row['committee_name'];
?>
<a id=committee href=""><span><?= $committee_name ?></span></a>
<?php } ?>
Now what i want is as soon as 1 of the committee is selected.. i want to take data from ward details as well as the committee id and name to the next page.
THis will provide much clear view
<input type=hidden id=committee_id value=<?=$committeeid?>>
<input type=hidden id=committee_name value=<?=$committee_name?>>
var committeeid = $("#committee_id").val();
$("#committee").click(function(){
window.open("index.php?id="+committeeid+"", '_blank');
});
I used input tag to store the value and marked it as hidden. Then on click of committee item i fetched data from input and use it accordingly.
Hi I have a problem with this one. Instead of having a result of uniqueID = value I'm having a result like this uniqueID = undefinedvalue
JavaScript Code
function read(a){
var html;
if(a.indexOf("http://") === 0 || a.indexOf("https://") === 0)
html+="<a target='_blank' href='"+a+"'>"+a+"</a><br>";
html+=htmlEntities(a);
var audio = new Audio('lib/beep.ogg');
audio.play();
var uniqueID = document.getElementById("mapo").innerHTML= html;
window.location.href = "http://localhost/QR_JEFF/server.php?uniqueID=" + uniqueID;
}
PHP Code
$db = mysqli_connect('localhost', 'root','','suffrage');
$uniqueID = $_GET['uniqueID'];
$query = "SELECT * FROM applicant_table WHERE unique_id='$uniqueID'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$logged_in_user = mysqli_fetch_assoc($results);
if($logged_in_user['validation_status'] == 'Verified' && $logged_in_user['voting_status'] == 'No'){
$_SESSION['unique_id'] = $id;
$_SESSION['validation_status'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: wow.html');
}
}
This is the output in my URL. check this out to see the problem
Thank you in advance. :)
var uniqueID = document.getElementById("mapo").innerText;
console.log(uniqueID);
/*var uniqueID = document.getElementById("mapo").value;*/
<div id="mapo">123456789</div>
<!--<input id="mapo" type="hidden" value="123456789" />-->
I solved my own problem. This is the solution.
function read(a){
var html=htmlEntities(a);
var audio = new Audio('lib/beep.ogg');
audio.play();
var uniqueID = document.getElementById("mapo").innerHTML= html;
window.location.href = "http://localhost/QR_JEFF/server.php?uniqueID=" + uniqueID;
}
I realized that the var=html dont have any value and when I concatenate it to my htmlEntities it resulted into an undefined index. So I remove it and place the htmlEntities straight to the var html. :)
I'm making a stock table, where the user can input a number of stock to issue out, and this function is to verify, for each member of the table, whether or not the quantity of stock in the database is sufficient to make the issue.
When I breakpoint through the code on Chrome, it seems to never hit the inside code of the $.post until after its finished looping through the table. And by then the data that is supposed to be passed to AddIssue just becomes 1 and 10 and nothing else.
It's starting to drive me mad, so I'd appreciate a pointer on what I'm doing wrong here
var issueArray =[];
function VerifyIssue()
{
var numRows = document.getElementById("searchTable").rows.length - 2;
var running = true;
var str1 = "issue_";
while(running == true)
{
if (numRows < 0 && running == true)
{
running = false;
//insert code for success
break;
}
else if (running == false)
{
break;
}
var str2 = numRows;
var comb = str1.concat(str2);
var issue_element = document.getElementById(comb);
var q = issue_element.value;
var i = issue_element.name;
var idd = i.replace("issue_", "");
var trimId = idd.trim();
$.post
(
'includes/issueCheck.php',
{
id: trimId,
issueQuantity: q
},
function(result)
{
alert(result);
if (result < 0)
{
alert("One of more quantities inputted are greater than held in stock");
running = false;
}
if (result > 0)
{
addIssue(trimId, q);
}
}
);
numRows = numRows - 1;
}
alert(issueArray);
}
function addIssue(issueID, quant)
{
var item = {};
item.label = issueID;
item.value = quant;
issueArray.push(item);
}
This is the PHP that is called by the $.post
<?php
$server = 'SQL2008';
$connectionInfo = array( "Database"=>"rde_470585");
$conn = sqlsrv_connect($server,$connectionInfo);
$false = -1;
$true = 1;
$id = $_POST['id'];
$quantity = $_POST['issueQuantity'];
$query = "Select Quantity FROM Stock WHERE StockID = ?";
$param = array($id);
$res = sqlsrv_query($conn, $query, $param);
$row = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC);
$dbQuantity = $row['Quantity'];
if ($dbQuantity < $quantity)
{
echo $false;
}
else if($dbQuantity >= $quantity)
{
echo $true;
}
Do you realise that your function(result){ ... } will only be called when the server response for your POST request is received? You have to build the code to take that delay into account.
I'm still working on this, reading up on Promises based on what #jojo said, but not sure if it's going to work for me.
How else can I make the javascript wait for a server response before resuming?
I have getpunch method in script and PHP also what i want is pass the hours:minuts between in_time and current time
This is my script :
AttendanceAdapter.method('getPunch', function(successCallBack) {
var that = this;
var object = {};
object['date'] = this.getClientDate(new Date()).toISOString().slice(0, 19).replace('T', ' ');
object['offset'] = this.getClientGMTOffset();
var reqJson = JSON.stringify(object);
var callBackData = [];
callBackData['callBackData'] = [];
callBackData['callBackSuccess'] = successCallBack;
callBackData['callBackFail'] = 'getPunchFailCallBack';
this.customAction('getPunch','modules=attendance',reqJson,callBackData);
});
This is my PHP :
public function getPunch($req){
$date = $req->date;
$arr = explode(" ",$date);
$date = $arr[0];
$employee = $this->baseService->getElement('Employee',$this->getCurrentEmployeeId(),null,true);
//Find any open punch
$attendance = new Attendance();
$attendance->Load("employee = ? and DATE_FORMAT( in_time, '%Y-%m-%d' ) = ? and (out_time is NULL or out_time = '0000-00-00 00:00:00')",array($employee->id,$date));
if($attendance->employee == $employee->id){
//found an open punch
return new IceResponse(IceResponse::SUCCESS,$attendance);
}else{
return new IceResponse(IceResponse::SUCCESS,null);
}}
using this when user try to punch_out in that form showing in_time i want show in that form how many hours they worked when they try to punch_out
I have the following data in an SQL database
cname, ccode, colour
Great Britain, GB, 1
Italy, IT, 1
France, FR, 1
Spain, ES, 1
How can I create a JavaScript object like below?
var countries = {"GB":1, "IT":1, "FR":1, "ES":1}
So far, I have the following code;
PHP
$query2 = "SELECT ccode,colour FROM country_data WHERE 1";
$result2 = mysql_query($query2);
if (!$result2) {
die('Invalid query: ' . mysql_error());
}
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[] = $r2;
}
JS
var colours = '<?php print json_encode($rows2); ?>';
Which returns:
[{"ccode":"GB","colour":"1"},{"ccode":"IT","colour":"1"},{"ccode":"FR","colour":"1"},{"ccode":"ES","colour":"1"}]
change this code
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[] = $r2;
}
to this one:
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[$r2['ccode']] = $r2['colour'];
}
change php code as told in others answers too and it should be like
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[$r2['ccode']] = $r2['colour'];
}
if it does not work then try this:
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2["'".$r2['ccode']."'"] = $r2['colour'];
}
if it also does not work then try this:
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[{$r2['ccode']}] = $r2['colour'];
}