Hi I am working on Project.
I want value from MySQL DB in Second input field on input of code in first input field.
Is it possible?

Recommended Answers

All 26 Replies

Go through the follwing code carefully. I have not used ajax here. Its simple php/javascript. I am assuming that your code,mf table is very small so i am storing its values in a javascript array, and using it for calculation. If your table is very large then u must learn ajax for retriving values from mysql. Make necessary changes whereever application

<script lang='javascript'>
arrmfrates = new Array();
<?php 
	/*here find code, mf from database and create one javascript array*/
	
	/* following loop will create javacript asscoicate array as following
	arrmfrates["c8620"] = 2;
	arrmfrates["c8621"] = 1;
	arrmfrates["c8645"] = 5;
	*/
    
    $result=mysql_query("select code,mf from mytable");
	while ($row=$mysql_fetch_array($result ))
	{
		
		echo "\n arrmfrates['c".$row['code']."] = ".$row['code'].";";

	}
?>

/*this function will be called when user enters code, to find mf value stored in javascript array, this mf value is copied to "a" field of database*/
function findmf(code)
{

	if(arrmfrates['c'+code]!=undefined)
		document.getElementById("a").value=arrmfrates['c'+code];
	else
		document.getElementById("a").value="";
}

function calculate()
{
 //e is previous
 //f is current
 //g is new is f-e
 //code is code
 //a is mf
 
 	previous=parseFloat(document.getElementById("e").value);
 	curr=parseFloat(document.getElementById("f").value);

	if(!isNaN(previous) && !isNaN(curr) )
	{
	 	if(curr>=previous)
			document.getElementById("g").value=curr-previous*(parseFloat(document.getElementById("a").value));
		else
			document.getElementById("g").value="";
	}
	else
	{
		document.getElementById("g").value="";
	}
}
</script>


<body>
<form>
code <input type="text" name="code" id="code" value=""  class="input"    onblur='javascript:findmf(this.value);' > <br>
a <input type="text" name="a" id="a" value=""  class="input"   readonly> <br>

e <input type="text" name="e" id="e" value="340"  class="input" onblur='javascript:calculate();' > <br>
f <input type="text" name="f" id="f" value=""  class="input"  onblur='javascript:calculate();' > <br>
g <input type="text" name="g" id="g" value=""  class="input"  readonly >
</form>

But my table have more then 5500 records.
and it will reach upto 8000 in next 3 years.
please guide me using ajax

You mean your code-mf table is having 5500 rows.

Yea its have 5500 codes(rows)

I think having 5500 records it will slow down the proccessing

$result=mysql_query("select code,mf from mytable");

Can I add where Clause in this Query so if code is 8645 then it gets value of MF only against 8645 and then store in mf feild(u named a)

Member Avatar for rajarajan2017

Yes you can add that in your query:

$result=mysql_query("select mf from mytable where code=$code");

But your issue is how to pass the code from javascript to php r8?

Yea , my issue to get value of mf from DB on input of code in HTML Form.
So How I can use where Clause?

I am using basic ajax. Now you have to create one php file getmf.php in the same folder where your current file is saved. copy following code to it. set database connection properly

<?php
	//set data base connection
    $result=mysql_query("select code,mf from mytable where code='{$_REQUEST['code']}'");
    $row=$mysql_fetch_array($result);
    echo $row['mf'];//return mf value for requested code;
?>

then add new javascript function and other changes as given below to current file. Now if you are not comfortable with the code then I suggest you to learn some ajax tutorials and then try to solve your problem

<script lang='javascript'>
function findmf(str)
{
	if (str=="")
	{
		document.getElementById("a").value="";
		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("a").value=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","getmf.php?code="+str,true);
	xmlhttp.send();
}

function calculate()
{
 //e is previous
 //f is current
 //g is new is f-e
 //code is code
 //a is mf
 
 	previous=parseFloat(document.getElementById("e").value);
 	curr=parseFloat(document.getElementById("f").value);

	if(!isNaN(previous) && !isNaN(curr) )
	{
	 	if(curr>=previous)
			document.getElementById("g").value=curr-previous*(parseFloat(document.getElementById("a").value));
		else
			document.getElementById("g").value="";
	}
	else
	{
		document.getElementById("g").value="";
	}
}
</script>


<body>
<form>
code <input type="text" name="code" id="code" value=""  class="input"    onblur='javascript:findmf(this.value);' > <br>
a <input type="text" name="a" id="a" value=""  class="input"   readonly> <br>

e <input type="text" name="e" id="e" value="340"  class="input" onblur='javascript:calculate();' > <br>
f <input type="text" name="f" id="f" value=""  class="input"  onblur='javascript:calculate();' > <br>
g <input type="text" name="g" id="g" value=""  class="input"  readonly >
</form>
Member Avatar for rajarajan2017

I too expecting a solution to pass javascript variable to php code!!!!

When I put this code in Scrip Tag it does not show anything(show blank page)

arrmfrates = new Array();
<?php 
	/*here find code, mf from database and create one javascript array*/
 
	/* following loop will create javacript asscoicate array as following
	arrmfrates["c8620"] = 2;
	arrmfrates["c8621"] = 1;
	arrmfrates["c8645"] = 5;
	*/
 
    $result=mysql_query("select code,mf from mytable");
	while ($row=$mysql_fetch_array($result ))
	{
 
		echo "\n arrmfrates['c".$row['code']."] = ".$row['code'].";";
 
	}
?>

You refer to my last post on the first page of this thread, code is using AJAX.

Now if you want to do it in above code. then this code will set 5500 array elements in javascript, which is not good practice. Also you need to set database connection before executing the query. also have you changed. mytable to the actual tablename in your script.

this is my getmf.php page

<?php
			include("config.php");
    $result=mysql_query("select SiteID,MFR from eims_info where SiteID='{$_REQUEST['a']}'");
    $row=$mysql_fetch_array($result);
    echo $row['MFR'];//return mf value for requested code;
?>

and this is my eims.php page

<script type="text/javascript">


function findmf(str)
{
	if (str=="")
	{
		document.getElementById("mf").value="";
		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("mf").value=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","getmf.php?course="+str,true);
	xmlhttp.send();
}
function calculate()
{
 //e is previous
 //f is current
 //g is new is f-e
 //course is code
 //mf is mf
 
 	previous=parseFloat(document.getElementById("e").value);
 	curr=parseFloat(document.getElementById("f").value);
 
	if(!isNaN(previous) && !isNaN(curr) )
	{
	 	if(curr>=previous)
			document.getElementById("g").value=curr-previous*(parseFloat(document.getElementById("mf").value));
		else
			document.getElementById("g").value="";
	}
	else
	{
		document.getElementById("g").value="";
	}
}
</script>

and these are fields but its not working
Its giving result

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

in MF feild
and NaN in the g feild
Feilds are like

<input type="text" name="a" id="course"  value=""  class="input" onblur='javascript:findmf(this.value);'/> </td>
<input type="text" name="mf" id="mf"  value=""  class="input" readonly/>
<input type="text" name="e" id="e" value=""  class="input" onblur='javascritp:calculate();'/> 

<input type="text" name="f" id="f" value=""  class="input" onblur='javascritp:calculate();'/> 
<input type="text" name="g" id="g" value=""  class="input"/>

xmlhttp.open("GET","getmf.php?course="+str,true);

here you are passing course and in your getmf you are using Requst[a]
so you should use
....where SiteID='{$_REQUEST}'


you open browser and run yourserver/getmf.php?course=8645

now see what the browser is returning, is it giving curret value for 8645 or not.

also in getmf.php file delete extra space and lines before <?php tag and after ?> tag

Showing error on line 12
Fatal error: Function name must be a string in C:\xampp\htdocs\getmf.php on line 12

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
			include("config.php");
    $result=mysql_query("select SiteID,MFR from eims_info where SiteID='{$_REQUEST['course']}'");
    $row=$mysql_fetch_array($result);
    echo $row['MFR']; //return mf value for requested code;
?>
</body>
</html>

and showing this in MF

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head>  <body> <br /> <b>Fatal error</b>:  Function name must be a string in <b>C:\xampp\htdocs\getmf.php</b> on line <b>12</b><br />

Showing error on line 12
Fatal error: Function name must be a string in C:\xampp\htdocs\getmf.php on line 12

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
			include("config.php");
    $result=mysql_query("select SiteID,MFR from eims_info where SiteID='{$_REQUEST['course']}'");
    $row=$mysql_fetch_array($result);
    echo $row['MFR']; //return mf value for requested code;
?>
</body>
</html>

Can I use While loop for getting value

Please do not use html code only keep php tags whatever i have given below.

Also there is one error in line $row=$mysql_fetch_array($result);
remove $ sign from the begining of mysql_fetch_array
so correct line is

$row=mysql_fetch_array($result);

following code is correct code

<?php			
include("config.php");    
$result=mysql_query("select SiteID,MFR from eims_info where SiteID='{$_REQUEST['course']}'");    
$row=mysql_fetch_array($result);    
echo $row['MFR']; //return mf value for requested code;
?>

REMOVE ALL HTML CODE your script should return only value of mf

ayesha789?
Would you like to use select instead of the ordinary textbox. Its simple getting values from DB using select field.

Reply if you need the code.

But Against one id I only have one mf code. why user choose????

Yea I changed it as u mentioned but not worked, then I go for j query and j query autolist solved my problem. can I share with others?

yes. Pls share with us

I am giving you every code segment after proper testing. I dont know why its not working there. any ways post your final code for others.

Thanks a lot urtrivedi , my problem is solved.
Can I share JQuery code?

Thanks a lot urtrivedi , my problem is solved.
Can I share JQuery code?

download J query from there site and use following code
I get_list from 2 table using getdata.php

<?php
		include("../config.php");

$q = strtolower($_GET["q"]);
if (!$q) return;

$sql = "select DISTINCT en.SiteID,inf.RefNo,inf.MFR,inf.MFMDI,inf.MFKV
FROM eims_north as en
Inner JOIN eims_info as inf ON en.SiteID = inf.SiteID
WHERE en.SiteID LIKE '%$q%'";
$rsd = mysql_query($sql);
if (!$rsd)
  {
  die('Could not connect: ' . mysql_error());
  }

while($rs = mysql_fetch_array($rsd)) {
    $SiteID = $rs['SiteID'];
    $refno = $rs['RefNo'];
	$mfr = $rs['MFR'];
	$mfm = $rs['MFMDI'];
	$mfk = $rs['MFKV'];
    echo "$SiteID|$refno|$mfr|$mfm|$mfk\n";
}

?>

I use this Jquey code to get data

<script type="text/javascript">




$(document).ready(function() {




    $("#course").autocomplete("Jqueryhaseeb/getdata.php", {

        width: 200,
	    
        matchContains: true,
        selectFirst: false

    });

$("#course").result(function(event, data, formatted) {

        $("#course_val").val(data[1]);
		$("#mfr").val(data[2]);
		$("#mfm").val(data[3]);
		$("#mfk").val(data[4]);
    });


});
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.