how to pass the edited value to database - javascript

i am making the html table with an inline edit using http://www.iwebux.com/inline-edit-using-ajax-in-jquery/ i try to pass the edited value to database but its not passing,i want to pass the id and the edited value to database below is my code.Please anyone guide me i am new to programming thanks.
ajax
$(document).ready(function () {
$('td.edit').click(function () {
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
$(this).addClass('ajax');
$(this).html('<input id="editbox" size="' + $(this).text().length + '" type="text" value="' + $(this).text() + '">');
$('#editbox').focus();
}
);
$('td.edit').keydown(function (event) {
arr = $(this).attr('class').split(" ");
if (event.which == 13) {
$.ajax({
type: "POST",
url: "config.php",
data: "value=" + $('.ajax input').val() + "&rowid=" + arr[1] + "&region=" + arr[2],
success: function (data) {
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
}
});
}
}
);
$('#editbox').live('blur', function () {
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
});
});
Html
<body>
<table cellpadding="15">
<tr class="heading" bgcolor="#ccc">
<th>region</th>
<th>country</th>
<th style="width:285px;">networkname</th>
<th>mcc</th>
<th>mnc</th>
<th>mnp</th>
</tr>
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'fms';
$dbPassword = 'xxxxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$sql = mysql_query("SELECT * FROM supplierprice");
while($row=mysql_fetch_array($sql))
{
echo '<tr '.$row['id'].' >';
echo "<td class='edit' >" . $row['region'] . "</td>";
echo "<td class='edit' >" . $row['country'] . "</td>";
echo "<td class='edit' >" . $row['networkname'] . "</td>";
echo "<td class='edit' >" . $row['mcc'] . "</td>";
echo "<td class='edit' >" . $row['mnc'] . "</td>";
echo "<td class='edit'>" . $row['mnp'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
config.php
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'fms';
$dbPassword = 'xxxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
print $_POST['rowid'];
if($_POST['rowid'])
{
$id=mysql_escape_String($_POST['id']);
$region=mysql_escape_String($_POST['region']);
$country=mysql_escape_String($_POST['country']);
$networkname=mysql_escape_String($_POST['networkname']);
$mcc=mysql_escape_String($_POST['mcc']);
$mnc=mysql_escape_String($_POST['mnc']);
$mnp=mysql_escape_String($_POST['mnp']);
$sql = "update supplierprice set region='$region',country='$country',networkname='$networkname',mcc='$mcc',mnc='$mnc',mnp='$mnp' where id='$id'";
mysql_query($sql);
print " $sql";
}
?>

Related

Sum of Column in HTML and PHP

I am needing to get the total of a row from a MySQL data base that is listed in an HTML table.
I have tried using separate sql select statements with the as statement for the new row (i.e. SELECT SUM(product_price) AS order_total FROM orderplace WHERE orderplace_number = '$item') as well as using it in the original select statement.
Here is my original code without the total added up. I would like the total in a different row spanning across all columns. Thanks in advance!
<table width="100%">
<tr>
<th>Product ID</th>
<th>Item Name</th>
<th>Description</th>
<th>Price</th>
<th>Actions</th>
</tr>
<?php
$cart = $_COOKIE['crochetdamour'];
if ($cart) {
$i = 1;
include('includes/dbc.php');
$items = explode(',', $cart);
foreach ($items AS $item) {
$sql = "SELECT * FROM orderplace WHERE orderplace_number = '$item'";
$result = mysqli_query($con, $sql);
if ($result == false) {
$mysql_error = mysqli_error($con);
echo "There was a query error: $mysql_error";
} else {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr><td align="left" id="prodid" name="prodid">' . $row['product_id'] . '</td>';
echo '<td align="left" id="prodname" name="prodname">' . $row['product_name'] . '</td>';
echo '<td align="left" class="desctd" id="proddesc" name="proddesc">' . $row['product_size'] . ', ' . $row['product_gender'] . ', ' . $row['product_theme'] . ', ' . $row['product_specs'] . '</td>';
echo '<td align="left" id="prodprice" name="prodprice">' . $row['product_price'] . '</td>';
echo '<td align="left">Remove From Cart</td></tr>';
} //end while
$i++;
} //end else
} //end foreach
} //end if
?>
</table>
You have to this:
foreach ($items AS $item) {
$sum = 0; //<-------------
$sql = "SELECT * FROM orderplace WHERE orderplace_number = '$item'";
$result = mysqli_query($con, $sql);
if ($result == false) {
$mysql_error = mysqli_error($con);
echo "There was a query error: $mysql_error";
} else {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr><td align="left" id="prodid" name="prodid">' . $row['product_id'] . '</td>';
echo '<td align="left" id="prodname" name="prodname">' . $row['product_name'] . '</td>';
echo '<td align="left" class="desctd" id="proddesc" name="proddesc">' . $row['product_size'] . ', ' . $row['product_gender'] . ', ' . $row['product_theme'] . ', ' . $row['product_specs'] . '</td>';
echo '<td align="left" id="prodprice" name="prodprice">' . $row['product_price'] . '</td>';
echo '<td align="left">Remove From Cart</td></tr>';
$sum += $row['product_price']; //<--------------
} //end while
$i++;
} //end else
echo $sum; //<----------------
} //end foreach
Hope it will helps

Why are my results not showing up from my database?

I have a program that displays authors book code and book title using php and
AJAX technology, but for some reason the data is not appearing in the table. I know my SQL code is correct as our instructor gave us the code for that, but something is preventing the data from appearing in the table. Any tips or suggestions would be appreciated!
<body>
<?php
$authorid = 0;
$authorid = (int) $_GET['authorid'];
if ($authorid > 0) {
require_once('dbtest.php');
$query = "SELECT * FROM author";
$r = mysqli_query($dbc, $query);
if (mysqli_num_rows($r) > 0) {
$row = mysqli_fetch_array($r);
} else {
echo "Title Not Returned<br>";
}
echo "<table border='1'><caption>Titles for </caption>";
echo "<tr>";
echo "<th>Book Code</th>";
echo "<th>Book Title</th>";
echo "</tr>";
$q2 ="SELECT wrote.author_number As ANo, wrote.book_code As BookCd, book.book_title As Title ";
$q2 .= " FROM wrote, book ";
$q2 .= " WHERE wrote.book_code=book.book_code ";
$q2 .= " AND wrote.author_number = ' ' ";
$q2 .= " ORDER BY book.book_title";
$r2 = mysqli_query($dbc, $q2);
$row = mysqli_fetch_array($r2);
while ($row) {
echo "<tr>";
echo "<td>" .$row['BookCd']. "</td>";
echo "<td>" .$row['Title']. "</td>";
echo "</tr>";
$row = mysqli_fetch_array($r2);
}
echo "</table>";
} else {
echo "<p>No Author ID from prior page</p>";
}
?>
</form>
</body>
The suspicious line is: AND wrote.author_number = ' '
Why is it empty?
Put a check after the second query:
$r2 = mysqli_query($dbc, $q2);
if (mysqli_num_rows($r2) > 0) {
echo "rows are Returned<br>";
} else {
echo "rows are Not Returned<br>";
}
$row = mysqli_fetch_array($r2);

Cannot read property value of null. No value on onclick

I have a webpage that can update a table live with AJAX. I have some problems with the onlick function.
Whenever I click the update button it shows on console
Cannot read property 'value' of null(…)bb # manageaccounts.php:184 onclick # manageaccounts.php:229
I checked it in my update.php file but the code is just fine. I tried to change the onclick function from onclick="(this.id)" to onclick="(this.firstname)" but none worked.
manageaccounts.php
<body>
<div class="wrapper">
<div id="data" class="table"></div>
</div>
<script type="text/javascript">
data();
function data() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "update.php?status=disp", false);
xmlhttp.send(null);
document.getElementById("data").innerHTML = xmlhttp.responseText;
}
function aa(a) {
firstid = "firstname" + a;
txtfirstid = "txtfirst" + a;
var firstname = document.getElementById(firstid).innerHTML;
document.getElementById(firstid).innerHTML = "<input type='text' value='" + firstname + "' id='" + txtfirstid + "'>";
midid = "middlename" + a;
txtmidid = "txtmid" + a;
var middlename = document.getElementById(midid).innerHTML;
document.getElementById(midid).innerHTML = "<input type='text' value='" + middlename + "' id='" + txtmidid + "'>";
lastid = "lastname" + a;
txtlastid = "txtlast" + a;
var lastname = document.getElementById(lastid).innerHTML;
document.getElementById(lastid).innerHTML = "<input type='text' value='" + lastname + "' id='" + txtlastid + "'>";
addid = "address" + a;
txtaddid = "txtadd" + a;
var address = document.getElementById(addid).innerHTML;
document.getElementById(addid).innerHTML = "<input type='text' value='" + address + "' id='" + txtaddid + "'>";
gendid = "gender" + a;
txtgendid = "txtgend" + a;
var gender = document.getElementById(gendid).innerHTML;
document.getElementById(gendid).innerHTML = "<input type='text' value='" + gender + "' id='" + txtgendid + "'>";
contid = "contact" + a;
txtcontid = "txtcont" + a;
var contact = document.getElementById(contid).innerHTML;
document.getElementById(contid).innerHTML = "<input type='text' value='" + contact + "' id='" + txtcontid + "'>";
updateid = "update" + a;
document.getElementById(a).style.visibility = "hidden";
document.getElementById(updateid).style.visibility = "visible";
}
function bb(b) {
var firstid = "txtfirst" + b;
var firstname = document.getElementById(firstid).value;
var midid = "txtmid" + b;
var middlename = document.getElementById(midid).value;
var lastid = "txtlast" + b;
var lastname = document.getElementById(lastid).value;
var addid = "txtadd" + b;
var address = document.getElementById(addid).value;
var gendid = "txtgend" + b;
var gender = document.getElementById(gendid).value;
var contid = "txtcont" + b;
var contact = document.getElementById(contid).value;
update_value(b, firstname, middlename, lastname, address, gender, contact);
document.getElementById(b).style.visibility = "visible";
document.getElementById("update" + b).style.visibility = "hidden";
document.getElementById("firstname" + b).innerHTML = firstname;
document.getElementById("middlename" + b).innerHTML = middlename;
document.getElementById("lastname" + b).innerHTML = lastname;
document.getElementById("address" + b).innerHTML = address;
document.getElementById("gender" + b).innerHTML = gender;
document.getElementById("contact" + b).innerHTML = contact;
}
function update_value(id, firstname, middlename, lastname, address, gender, contact) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "update.php?id=" + id + " & firstname=" + firstname + " & middlename=" + middlename + " & lastname=" + lastname + " &address=" + address + " & gender=" + gender + " &contact=" + contact + " & status=update", false);
xmlhttp.send(null);
}
function delete1(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "update.php?id=" + id + " & status=delete", false);
xmlhttp.send(null);
data();
}
</script>
</body>
update.php has the null value onclick <input type="button" id="update<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="UPDATE" style="visibility: hidden;" onclick="bb(this.firstname)">
<?php
$conn = mysqli_connect('localhost', 'root','root', 'realestate');
if (!$conn) {
die("Sorry we're having some problems with the database. :(".mysqli_connect_error());
}
$status = $_GET["status"];
if ($status == "disp") {
$sql1 = "SELECT * FROM agents"; // check the change ere
$result=mysqli_query($conn, $sql1);
echo "<table>";
echo "<tr>
<th>Agent ID</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>Address</th>
<th>Gender</th>
<th>Contact</th>
<th>Action</th>
</tr>";
while($row=mysqli_fetch_assoc($result)) {
echo "<tr class='da'>";
echo "<td>"; echo $row["id"]; echo "</td>";
echo "<td>"; ?><div id="firstname<?php echo $row["id"]; ?>"><?php echo $row["firstname"]; ?></div><?php echo "</td>";
echo "<td>"; ?><div id="middlename<?php echo $row["id"]; ?>"><?php echo $row["middlename"]; ?></div><?php echo "</td>";
echo "<td>"; ?><div id="lastname<?php echo $row["id"]; ?>"><?php echo $row["lastname"]; ?></div><?php echo "</td>";
echo "<td>"; ?><div id="address<?php echo $row["id"]; ?>"><?php echo $row["address"]; ?></div><?php echo "</td>";
echo "<td>"; ?><div id="gender<?php echo $row["id"]; ?>"><?php echo $row["gender"]; ?></div><?php echo "</td>";
echo "<td>"; ?><div id="contact<?php echo $row["id"]; ?>"><?php echo $row["contact"]; ?></div><?php echo "</td>";
echo "<td>"; ?><button id="<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" onclick="delete1(this.id)">DELETE</button><?php echo "</td>";
echo "<td>"; ?>
<input type="button" id="<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="EDIT" onclick="aa(this.id)">
<input type="button" id="update<?php echo $row["id"]; ?>" name="<?php echo $row["id"]; ?>" value="UPDATE" style="visibility: hidden;" onclick="bb(this.firstname)">
<?php echo "</td>";
echo "</tr>";
}
echo "</table>";
}
if ($status == "update") {
$id = $_GET["id"];
$first = $_GET["firstname"];
$mid = $_GET["middlename"];
$last = $_GET["lastname"];
$add = $_GET["address"];
$gend = $_GET["gender"];
$cont = $_GET["contact"];
$first = trim($first);
$mid = trim($mid);
$last = trim($last);
$add = trim($add);
$gend = trim($gend);
$cont = trim($cont);
$sql2 = "UPDATE agents SET firstname='$first', middlename='$mid', lastname='$last', address='$add', gender='$gend', contact='$cont' WHERE id=$id";
$result = mysqli_query($conn, $sql2);
}
if ($status == "delete") {
$id = $_GET["id"];
$sql3 = "DELETE FROM agents WHERE id=$id";
$result = mysqli_query($conn, $sql3);
}
?>
In your function bb, you are doing:
var firstid="txtfirst"+b;
var firstname = document.getElementById(firstid).value;
You are calling the function like:
onclick="bb(this.firstname)"
Now, this.firstname would be undefined as this is pointing to your button and the button does not have a property called firstname.
Also, when you do like
onclick="(this.id)"
Then inside your function, firstid would become "txtfirstupdate1" as the id of your button will be update + id and not just id. So, document.getElementById(firstid) would be actually null and that's the reason why you are getting this error.

how to call mysql update query using onclick function

<?php
include_once("db.php");
$result=mysql_query("SELECT * FROM stu WHERE receiver='DM4'");
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ptype'] . "</td>";
echo "<td>" . $row['source'] . "</td>";
echo "<td>" . $row['letterno'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['descrip'] . "</td>";
echo "<td>" . $row['receiver'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td><a href='vex.php?uid={$row['letterno']}' id='id' onClick='addfavourite()'>.{$row['title']}.</a></td>";
//echo "<td><a href='update.php?id={$row['id']}'>Update</a></td>";
echo"<td><img style='width:100px;higth:150px;' src='upload/{$row[image]}'></td>";
addfavourite();
echo addfavourite();
function addfavourite() {
$ide=$_GET['uid'];
//echo $ide;
$sql = "SELECT * FROM stu WHERE letterno = '$ide'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if($row){
$newfav = "UPDATE stu SET open = 1 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);}
else{
$newfav = "UPDATE stu SET open = 0 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);}
}
}
echo $ide;
?>
This is my error code.In this code,there is a fault with mysql query statement.it is not supported with the databse. but onclick() function is working.
You cannot directly execute a PHP function from a JavaScript event. Simply, cannot mix server side and client side logic but you can make two of them interact with each other.
In this case, you can create a JavaScript function which sends an AJAX request to a PHP page on your server containing the code of addfavourite().
A Demo
I haven't tested the code below but assuming that your code logic and rest stuff is correct, this might just work
demo.php
<script>
function addfavourite(uid){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
var url = "addfavourite.php"; // URL of your PHP file
var vars = "uid="+uid;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
<?php
include_once("db.php");
$result=mysql_query("SELECT * FROM stu WHERE receiver='DM4'");
echo "<table>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ptype'] . "</td>";
echo "<td>" . $row['source'] . "</td>";
echo "<td>" . $row['letterno'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['descrip'] . "</td>";
echo "<td>" . $row['receiver'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td><a href='#' id='id' onClick='addfavourite({$row['letterno']})'>.{$row['title']}.</a></td>";
echo"<td><img style='width:100px;higth:150px;' src='upload/{$row[image]}'></td>";
}
echo "</table>";
?>
<div id='status'></div>
addfavourite.php
<?php
if(!isset($_POST['uid'])) {
$ide = $_POST['uid'];
$sql = "SELECT * FROM stu WHERE letterno = '$ide'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row) {
$newfav = "UPDATE stu SET open = 1 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);
} else {
$newfav = "UPDATE stu SET open = 0 WHERE letterno = '$ide'";
$createfav = mysql_query($newfav);
}
if ($createfav) {
echo "<script>alert('Added to favourite successfully...')";
} else {
echo "<script>alert('Something went wrong...')";
}
}else{
echo "POST variable not set!";
}
?>
Some notes
You should use mysqli insted of mysql functions as it is deprecated and no longer supported
This here is a good video tutorial on AJAX https://www.developphp.com/video/JavaScript/Ajax-Post-to-PHP-File-XMLHttpRequest-Object-Return-Data-Tutorial

How to separate the ajax response data in javascript

i have a ajax function that is returning a set of values from php page.
i need to get the values to that is only required. how can i do this
ajax.js
function MakeRequest()
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
xmlHttp.open("GET", "ajax.php", true);
xmlHttp.send(null);
}
ajax.php
<?php
require_once('config.php');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM thr';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
echo "<table border='1'>";
while($row = mysql_fetch_assoc($retval))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['ther_name'] . "</td>";
echo "<td>" . $row['region'] . "</td>";
echo "<td>" . $row['phone_no'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td id='lat'>" . $row['corrd_lattitude'] . "</td>";
echo "<td id='lon'>" . $row['corrd_longitude'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($conn);
?>
i need only values of
$row['corrd_lattitude']
$row['corrd_longitude']
how to get the values of this
first you can fetch only required values from table..if i understood correctly you can use following query.
$sql = 'SELECT corrd_lattitude,corrd_longitude FROM thr';
it will return only tw0 column value from table.
$json = json_encode(array($row['corrd_lattitude'], $row['corrd_longitude']));
echo $json;
You could specify parameters with your request.
In GET method:
ajax.php?corrd_lattitude=true&corrd_longitude=true
and in php file you can check this in $_GET array ie:
<?php
[...]
echo "<table border='1'>";
while($row = mysql_fetch_assoc($retval))
{
echo "<tr>";
if ($_GET['id'] == true) {
echo "<td>" . $row['id'] . "</td>";
}
if ($_GET['ther_name'] == true) {
echo "<td>" . $row['ther_name'] . "</td>";
}
if ($_GET['region'] == true) {
echo "<td>" . $row['region'] . "</td>";
}
if ($_GET['phone_no'] == true) {
echo "<td>" . $row['phone_no'] . "</td>";
}
if ($_GET['address'] == true) {
echo "<td>" . $row['address'] . "</td>";
}
if ($_GET['corrd_lattitude'] == true) {
echo "<td id='lat'>" . $row['corrd_lattitude'] . "</td>";
}
if ($_GET['corrd_longitude'] == true) {
echo "<td id='lon'>" . $row['corrd_longitude'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysql_close($conn);
?>
Better is use json (or plain values) instead of html - your js should pack values into html. It's better for server, client side should build html. Sry for my english.
use json:-----------
=========================================
$sql = "SELECT * FROM thr";
$rs = mysql_query($sql);
$res = mysql_fetch_assoc($rs);
if(mysql_num_rows($rs)>0){
echo json_encode(array('lats'=>$res['corrd_lattitude'],'lngs'=>$res['corrd_longitude']));
}else{
echo json_encode(array('lats'=>'','lngs'=''));
}
here lats and lngs are simply varaiable name to store values inside them and get the value on other page from lats and lngs varaibale.

Categories