www.webdeveloper.com
Results 1 to 4 of 4

Thread: Get sum with php

  1. #1
    Join Date
    Dec 2011
    Posts
    39

    Get sum with php

    Dear Sir, I have following codes

    Code:
    <html>
      <head>
        <title>Javascript function </title>
    	
        <style type="text/css">
    	.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 ">Javascript Function</h1>
    	<table border="0" cellspacing="1" cellpadding="1" width="25%"> 
        	<form name="form1" action="textboxes.php" method="Post">
        <tr><td> First</d><td width="20px"><input type="text" name="text1"  value=""></td></tr>
          <tr><td> Second</d><td><input type="text" name="text2"  value="" onBlur="hello()"></td></tr>
    	 <tr><td> Result</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 submitting FORM?</h4>
    
        </div>
    	</center>
        </body>
    </html>
    No problem, It works fine.
    I used java script to sum two numbers.
    Is it possible to add two numbers with php without using any submit button?

    If yes then please guide me.

    http://i41.tinypic.com/2rfev7m.jpg

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,404
    Assuming you're using PHP in the usual manner as a server-side processing mechanism, then something has to be submitted to the web server from the form on the browser. I suppose you could use some JavaScript to submit an AJAX request to the server and thus to a PHP script to do the addition, return the result, and populate the calculated value -- but that seems rather silly in that it would have to use JavaScript anyway, and it would be much simpler and faster to just do it all in JavaScript.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    39
    Thanks sir for helping, do you mean I should continue javascript for calculation on form then submit form to database?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,404
    Quote Originally Posted by tqmd1 View Post
    Thanks sir for helping, do you mean I should continue javascript for calculation on form then submit form to database?
    Well, that's a bit of a different question. If you just want to add the numbers together and show the result to the user, JavaScript is fine and a good choice. If you need the data to be stored in a DB, then you cannot count on JavaScript, as a hacker could emulate the request that would be sent to the server. Therefore, while you can use JS locally on the web page to provide an enhanced user experience (letting them see the sum before they submit the values), you would still want to submit the inputs to your PHP script and let it do the "official" calculations and then save the results in the DB. (Unless all you care about is the sum, and it doesn't matter to you what the values were that generated that sum?)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles