Get Sum

Dear Sir,

This is my first post in this forum. I hope to get help.

I have following codes

<html>
  <head>
    <title>Javascript function </title>
	
    <style type="text/css">

body
{
	margin-top:100px;
}

	.box
	{
		width:400px;
		background-color:#F0F8FF;
	}
	
	h4
	{
		color:#09F
	}
		
	</style>

<script type="text/javascript">
function hello(){
var xx=eval(form1.text1.value);
var yy=eval(form1.text2.value);
form1.text3.value=xx+yy
}
</script>
	
  </head>
    <body onLoad="form1.text1.focus()">
	<center>
    <div class="box">
	<h1 style="color:#2c80d3 ">Get Sum with Javascript</h1>
	<table border="0" cellspacing="1" cellpadding="1" width="25%">
    	<form name="form1" action="textboxes.php" method="Post">
    <tr><td> <strong>First
      </d>
    </strong>
    <td width="20px"><input type="text" name="text1"  value=""></td></tr>
      <tr><td> <strong>Second</strong>
        </d><td><input type="text" name="text2"  value="" onBlur="hello()"></td></tr>
	 <tr><td><strong> Result</strong>
	   </d><td><input type="text" name="text3"  value="" disabled=""></td></tr>
    </form>
	</table>
	<h4>Enter any digit in text1 and text2 and see result in text3</h4>
	<h4>Is it possible to do above with php without using submit button?</h4>

    </div>
	</center>
    </body>
</html>

I used Javascript to get sum of two numbers and now want get sum with php but without sumbit button.
Please help

Hi tqmd1,

Welcome to sitepoint forums. I hope your stay here is a pleasant one.

I am suspecting you need to use the OnExit event handler for the text boxes or maybe there is an OnChange handler. I may need to check the javascript docs and get back to you to be more specific.

PHP is processed on the server, so you do need to send the data back to the server to get the result. This basically requires you to submit the form. It may be possible to use Ajax to send the data to the server and to fetch back the result, but that still means you are using JS.

Ooops, I read the question wrong. I agree with ralph.m on this one.

Thanks for helping. Actually I am trying to get data from database without submitting form.
I have following codes

<?php
global $con;
require_once("connect.php");
$productValue="";
$productWeight="";
$productCode = "";
if(isset($_POST['button1']))
    {
        // Get values from form
        $sno =$_POST['txtsno'];
        $record_check ="SELECT * FROM test WHERE sno = '$sno' ";
        $result=mysqli_query($con, $record_check);
        $row = mysqli_fetch_array($result);
        if(!$row)

                    die ('No record Found');
	        else {
            $productValue = $row['packing'];
            $productWeight = $row['weight'];
            $productCode = $sno;
        }
    }
?>
<!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>Display data in textboxes</title>
<style type="text/css">
html {
overflow:auto;
}
body {
background-color:#FFFFFF;
margin:0 auto;
}
#mypopup
{
float: left;
width: 250px; height: 350px;
background: #90bade;
border: 1px solid #069;
text-align:center;
padding:2px;
margin-top:150px;
margin-left:100px;
overflow:auto;
}
#header
{
background-color:#3399FF;
background-position:left center;
line-height:25px;
font-size:22px;
color:#FFFF33;
font-weight:600;
border-bottom:1px solid #6699CC;
padding:10px;
}
</style>
</head>
<body>
<center>
  <div id="mypopup">
    <div id="header">Search Data</div>
    <div style="margin-top:80px;">
     <form name="form1" action="#" method="post">
        <table border=0; cellpadding="1" cellspacing="1" bgcolor="#CCFFFF" align="center" >
          <tr>
            <td>Code</td>
            <td width="50px"><input type="text" name="txtsno" id="txtsno" value="<?php  echo $productCode ; ?>" title="Enter product code" onkeypress="validate(event)" ;  onfocus="this.select()" /></td>
          </tr>
          <tr>
            <td>Product</td>
            <td><input type="text" name="txtpro" value="<?php echo $productValue; ?>" title="Enter product name" ></td>
          </tr>
          <tr>
            <td>Weight</td>
            <td><input type="text" name="txtwet" value="<?php echo $productWeight; ?>" title="Enter product weight" onfocus="this.select()" ></td>
          </tr>
        </table>
        <div style=text-align:center;margin-top:20px;>
          <input type="submit" name="button1" value="Display" >
          <input type="reset" name="button2" value="Clear" >
        </div>
      </form>
    </div>
  </div>
</center>
<?php
        mysqli_close($con);
?>
</body>
</html>

The code works fine as seen in image

But after entering SNO in text1, I have to press submit button everytime.

Is it possible to when I enter any sno in textbox then it must display result immediately?

Yes, see answers #2 and #3. You’ll need to use JS and AJAX to send the data to the PHP script and to show the result that the server will send back to the browser

Thanks, sir please guide me more how to use JS and AJAX with my codes to get required result.

I shall be very thankful to you.

Well, I would recommend the jquery ajax library. There is quite a bit to explain but I found this on Google :

http://www.cleverweb.nl/php/jquery-ajax-call-tutorial/