Calculate two input values of fetched data using jquery - javascript

The code below displays a table of data in input box based on drop down selection. Then I want to multiply row1 and row2 values and should display in row3. But the calculation between row1 and row2 is not working using jQuery. Need help!
display.php
<!DOCTYPE html>
<html lang="en">
<head>
<link href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
<script type="text/javascript">
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?q="+str,true);
xmlhttp.send();
}
}
$(document).ready(function() {
$('#form').on('keyup', '.test1', calcTotal) ;
$('#form').on('keyup', '.test2', calcTotal) ;
function calcTotal() {
var $row2 = $(this).closest('tr'),
test_row = $row2.find('.test1').val(),
test_row2 = $row2.find('.test2').val(),
total = test_row * test_row2;
$row2.find('.test3').val(total);
}
});
</script>
</head>
<body>
<form name="form_name" method="post" id="form_name" action="" >
<select name="select_name" id="select_name" onchange="showUser(this.value)">
<option value="22">22</option><option value="33">33</option></select>
<div id="txtHint"></div>
</form>
</body>
</html>
ajax.php
<?php
$q = $_GET['q'];
include('connection.php');
$sql="SELECT * from clients where value='".$q."' ";
$result = mysql_query($sql);
echo "<table id='form'>
<tr>
<th>row1</th>
<th>row2</th>
<th>row3</th>
</tr>";
while($row = mysql_fetch_array($result)) {
$row1=$row['row1'];
$row2=$row['row2'];
$row3=$row['row3'];
echo "<tr>";
echo "<td><input type='text' name='test1[]' class='test1' value='$row1' /></td>";
echo "<td><input type='text' name='test2[]' class='test2' value='$row2' /></td>";
echo "<td><input type='text' name='test3[]' class='test3' value='$row3'/></td>";
echo "</tr>";
}
echo "</table>";
?>

Replace these two line
$('#form').on('keyup', '.test1', calcTotal) ;
$('#form').on('keyup', '.test2', calcTotal) ;
With this
$(document).on('keyup', '.test1, .test2', calcTotal) ;
Because #form was added in the DOM after it's construction.

I think
$('#form').on('keyup','.test1',calcTotal) ;
should be replaced with
$('#form').on('keyup', '#test1', calcTotal) ;
as test1 is id of input and not class.

Related

How to filter the talbe using select option in php and mysql

I want to use the select option to update the table. these are the code and when I try it, no data have been display to table..im using js but still nothing happens..it only display the fields but no data in it. help me pls.
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">150-2012-00007</option>
<option value="2">120-2013-001</option>
<option value="3">130-2012-0022</option>
<option value="4">130-2012-0554</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
</body>
</html>
this is the code for the getuser.php
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'ofes'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn)
{
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}else{
echo "";
}
$sql="SELECT * FROM tbl_student WHERE Id_number = '".$q."'";
$result = mysqli_query($conn,$sql);
echo "<table>
<tr>
<th>Name</th>
<th>Id number</th>
<th>Passwordr</th>
<th>Course</th>
<th>School Year</th>
<th>Semester</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['Id_number'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['school_year'] . "</td>";
echo "<td>" . $row['semester'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
basically when I choose from the select option the table will be updated automatically.

want to fetch conditional data using ajax

My Working scenario is, i have 4 types of Pen
1-Diamond
2-Gold
3- Bronze
4- Silver
i want when someone select Diamond Pen on the bottom input he will type quantity so if he select 1-Diamond Pen so the amount should be vary with each other, on the same way all 4 types pen rates should be vary.
My Code is
ajax.php
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<!-- <input type="text" name="users" onchange="showUser(this.value)"> -->
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Diamond</option>
<option value="2">Gold</option>
<option value="3">Bronze</option>
<option value="4">Silver</option>
</select>
<!-- <input type="submit" name="users"> -->
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','fm_all');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM all_company WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['province'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
The problem is only Drop Box value is working, i do not know how to attach input quantity box with it. please help.
Note: this code is working according to id when i select drop box value so it show according to id but i did not attach it with quantity.
You can achieve this by following way in case you don't want for perform any operation on form submit
call showUser() on onkeyup for input and provide it some id may be quantity
call showUser() on onchange for select box and also provide it some id can be pen
function showUser(){
quantity = $("#quantity").val();
selected_pen = $("#pen").val();
if (quantity != null && selected_pen != null && typeof(quantity) !='undefined'){
your ajax calling code
}
}
you can call above function onchange for select box and on onkeyup in input textbox. But with above mentioned way it will call ajax every time there is change in your text field or select box.

How to update this script to respect JQuery AJAX syntax?

I have a functioning script that takes an option value, stores it in a variable q and sends an xmlhttp get call to a php script that queries a database a returns an html table with results. What I'm trying to do is update this to jQuery.ajax() with get method format ... any help is appreciated.
example_ajax_html_js.php:
<html>
<head>
<script src="//code.jquery.com/jquery-2.2.1.min.js"></script>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","/getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="Sophia">Sophia</option>
<option value="Daniel">Daniel</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
</body>
</html>
which calls getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
// connect to db
$sql="SELECT * FROM table WHERE first = '".$q."'";
$result = mysqli_query($web_dbi, $sql) or die("Error " . mysqli_error($web_dbi));
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>extrainfo1</th>
<th>extrainfo2</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['extrainfo1'] . "</td>";
echo "<td>" . $row['extrainfo2'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($web_dbi);
?>
</body>
</html>
Even easier than using jQuery's ajax method, I'd use the load method. Makes your job a piece of cake:
function showUser(str) {
if (str == "") {
$("#txtHint").html("");
return;
}
$("#txtHint").load("/getuser.php?q="+str);
}
That's it!
http://api.jquery.com/load/

HTML+JS send form data to PHP using POST

I'm really new to all HTML/JS/PHP but I want to create a simple login page thats connected to a database. User provide username&password, once sumbit button is clicked the webpage will change to user's information page.
So far, I have my data set up and my PHP function is working fine (I tested it with POSTMAN)
, however, I cannot get my HTML/JS working. I've been using the xmlhttp method to talk with PHP but failed
So, here is my html code
`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>login page</title>
<style type ="text/css">
h3{
text-align:center;
margin-top:20px;
}
p{
text-align:center;
}
div{
text-align:center;
}
</style>
</head>
<body>
<h3>Login Page</h3>
<form name="login">
<p>username: <input id="user" type="text" name="username" /></p>
<p>password: <input id="pass" type="password" name="password" /></p>
<p><input type="button" value ="Submit" onclick="showUser(document.getElementById('user').value, document.getElementById('pass').value)" /></p>
</form>
<br><br>
<div id="Info"><b>Student Infomation Table</b></div>
<script type="text/javascript">
function showUser(user, pass) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("Info").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","login.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("username="+user+"&password="+pass);
}
</script>
</body>
</html>
`
Here is the PHP
<?php
/*
* Checks the login
*/
include_once '/include/post_check.php';
// change header
header('Content-Type: application/json');
//check for post
if (checkPOST("username") && checkPOST("password")) {
$user_pass = $_POST['password'];
$user_name = $_POST['username'];
if ($user_pass == "" || $user_name == "") {
$temp = array("result" => "false", "reason" => "empty fields");
echo (json_encode($temp));
die();
}
} else {
$temp = array("result" => "false", "reason" => "not enough fields");
echo (json_encode($temp));
die();
}
// connect to MySQL
$con = mysqli_connect("localhost", "root", "xxx", "xxx");
// Check connection
if (mysqli_connect_errno()) {
// Print out reason
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if($user_name == "xxx" && $user_pass =="xxx"){
// write query (teacher's account)
$query = "SELECT *
FROM STUDENT";
}
$result = mysqli_query($con, $query);
echo "<table border='2'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
checkPOST() function
<?php
function checkPOST($field){
return (isset($_POST[$field]));
}
?>
the ERROR is simple one. You missed the method tag and ACTION tag in your form...without method and action tag inside FORM, you cant submit your form data.
so change form tag like this,
< form name="login" method="post" action="enter php you want to submit">
use this in html
Name:
use this in php
$name=$_POST ['name'];
mysql_connect(your server);
mysql_select_db (your database);
mysql_query (insert into your database);

jQuery don't work properly when element is retrieved by other element

This is the main page: Named 'index.php'
<html>
<head>
<script src='jquery.min.js' ></script>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="No match found";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
function adduser()
{
var fname=document.getElementsByName("fname")[0].value;
var lname=document.getElementsByName("lname")[0].value;
var age=document.getElementsByName("age")[0].value;
var ht=document.getElementsByName("ht")[0].value;
var job=document.getElementsByName("job")[0].value;
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","adduser.php
q="+fname+"&w="+lname+"&e="+age+"&r="+ht+"&t="+job,true);
xmlhttp.send();
}
$(document).ready(function(){
$("#hidelist").click(function(){
$("#list").hide("fast");
return false;
});
});
</script>
</head>
<body>
<button type='button' onclick='showUser(this.value)' id='getdata' value='1' >get data</button>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
This is the page when the button with id='getdata' is clicked. Named 'getuser.php'
<?php
error_reporting(0);
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','rathena','ajax_demo');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user";
$result = mysqli_query($con,$sql);
echo "<div id='list' ><table border='1' style='margin-top: 0px;' >
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table></div>";
echo "<input type='text' name='fname' value='Firstname' /><br />";
echo "<input type='text' name='lname' value='Lastname' /><br />";
echo "<input type='text' name='age' value='Age' /><br />";
echo "<input type='text' name='ht' value='Hometown' /><br />";
echo "<input type='text' name='job' value='Job' /><br />";
echo "<button style='display: non;' type='button' onclick='adduser()' >add user data</button>";
echo "<button style='display: non;' type='button' id='hidelist' >hide list</button>";
mysqli_close($con);
?>
But, what I want to achieve is, to hide the div with id='list' whenever I click the button with id='hidelist'. But the jQuery code dont catch the id of buttons with the page I retrieved. In this line:
$(document).ready(function(){
$("#hidelist").click(function(){
$("#list").hide("fast");
return false;
});
});
I appreciate for any ideas regarding this problem. Thanks :)
You'll have to delegate the event to the closest non-dynamic parent element, as the #hidelist element doesn't exist when you attach the event handler :
$(document).ready(function(){
$('#txtHint').on('click', '#hidelist', function(){
$("#list").hide("fast");
});
});

Categories

Resources