Tuesday, July 29, 2014

getting multiple values from text box without reloading and adding them in php

Yesterday i tried that the below code to execute the task
getting multiple values from text box without reloading and adding them in php...and i executed it....it would work definitely 

<html>
<body>
<form method="POST" action="multi.php">
<input type="text" id="t1" name="t1" placeholder="enter numbershere" onKeyup="return myFunc(this.value)">
<br>
<input type="hidden" id="hidden" name="hidden" >
<br>
<input type="submit" name="submit" value="submit" onClick="myFunc1()">
</form>
<script>
var i=0;
var arr=new array(5);
function myFunc(val){
                         
var x=document.getElementById("t1");
                         if(isNaN(x.value))
                         return false; //returns if value is a character//
                         else{
                         arr[i]=x.value;
i++;
x.value="";
                            return true;
                             }
}
function myFunc1()
{
var y=document.getElementById("hidden");
y.value=arr;
}
</script>
<?php
if(isset($_POST['submit']))
{
$val=$_POST['hidden'];
$sum=0;
$no1;
$no2;
$no3;
$no4;
$no5;
sscanf($val,"%d,%d,%d,%d,%d",$no1,$no2,$no3,$no4,$no5);
if($no1!="")
$sum+=$no1;
if($no2!="")
$sum+=$no2;
if($no3!="")
$sum+=$no3;
if($no4!="")
$sum+=$no4;
if($no5!="")
$sum+=$no5;
echo $sum;
}
?>

</body>
</html>

in above coding, i have used keyup() function to avoid multiple submit buttons or multiple times loading a whole page...if you guys have any idea...please share it...

No comments:

Post a Comment