How to include the output from database query in div using GET - javascript

i have index.html with the following code
<html>
<head><title>Testing</title></head>
<body>
<script>
var javascriptVariable = "abc";
window.location.href = "test_selected.php?name=" + javascriptVariable;
</script>
</body>
</html>
and test_selected file containing the code
<?php
$host = 'localhost';
$port = '5432';
$database = 'postgis';
$user = 'postgres';
$password = 'postgres';
$reg_name=$_GET['name'];
$connectString = 'host=' . $host . ' port=' . $port . ' dbname=' . $database .
' user=' . $user . ' password=' . $password;
$link = pg_connect ($connectString);
if (!$link)
{
die('Error: Could not connect: ' . pg_last_error());
}
$query="select * from table where name='{$reg_name}'";
$result = pg_query($query);
$i = 0;
echo '<html><body><table border="1"><tr>';
while ($i < pg_num_fields($result))
{
$fieldName = pg_field_name($result, $i);
echo '<td>' . $fieldName . '</td>';
$i = $i + 1;
}
echo '</tr>';
$i = 0;
while ($row = pg_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
pg_free_result($result);
echo '</table></body></html>';
?>
The output is displayed in a new page because i have used windows.location. Is it possible to display the output in the same page in a div instead of another new page.

include jquery in page. and replace
window.location.href = "test_selected.php?name=" + javascriptVariable;
with
$.ajax({
type:'GET'
url: "test_selected.php?name=" + javascriptVariable,
success: function(result){
$("#div_id").html(result);
},error:(error){
console.log(error);
});
replace div_id with the id of div you want to load into.

Related

Pure js count caracte input in case of multilanguage

I try to count the character inside different input in a loop. The count does not appear (not work.
I am not it's a good approach to that.
Thank you.
<?php
for ($i = 0, $n = \count($languages); $i < $n; $i++) {
echo '
<script>
var maxchar' . $i . ' = 70;
var i' . $i . ' = document.getElementById("SeoTitle' . $i . '");
var c' . $i . ' = document.getElementById("count");
c' . $i . '.innerHTML = maxchar' . $i . ';
i' . $i . '.addEventListener("keydown",count);
function count(e){
var len = i' . $i . '.value.length;
if (len >= maxchar' . $i . '){
e.preventDefault();
} else{
c' . $i . '.innerHTML = maxchar' . $i . ' - len-1;
}
}
?>
</script>';
HTML
echo HTML::inputField('categories_head_title_tag[' . $languages[$i]['id'] . ']', null, 'maxlength="70" size="77" id="SeoTitle' . $i . '"', false);
?>
<span id="count<?php echo $i; ?>"></span>
<?php
}
?>

Passing PHP Next Page Variables

I want to pass the php variable to the next page.
I tried many things, but I did not succeed.
Here is the attached case I tried.
index code
<script>
<?php
$con = mysqli_connect($db_host,$db_user,$db_passwd,$db_name);
mysqli_set_charset($con, "utf8");
$result = mysqli_query($con, "select * from StoreTable");
$n = 1;
while($row = mysqli_fetch_array($result)){
$name = $row['name'];
?>
positions_1.push({ content:
'<div class="wrap">' +
' <div>next page</div>' +
'</div>'
});
<?
$n++;
}
?>
</script>
next code
<?php
echo $name;
?>
url has been changed to a variable, but a blank screen is displayed. (Variables were not passed to the next page.)
Try this --
<script>
<?php
$con = mysqli_connect($db_host,$db_user,$db_passwd,$db_name);
mysqli_set_charset($con, "utf8");
$result = mysqli_query($con, "select * from StoreTable");
$n = 1;
while($row = mysqli_fetch_array($result)){
$name = $row['name'];
?>
positions_1.push({ content:
'<div class="wrap">' +
' <div>next page</div>' +
'</div>'
});
<?
$n++;
}
?>
</script>

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);

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 pass the edited value to database

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";
}
?>

Categories