how to calculate % and how to stop form from auto submit.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

how to calculate % and how to stop form from auto submit.

Post by adsegzy »

Hello Success-Oriented People,

I want to calculated 90% and a particular amount am having in my database. I named the column of the amount in the database abalance. this is the code i tried but its not working,

Code: Select all

$ninetypercent=(90/100*'$abalance');
What is the way out?

Secondly, am having a form where members can edit their account. When the member clicked on edit profile from his/her welcome page, the below page will be opened. my problem is that starting the form with teh IF function in order to stop the form from submitting automatically, whenever the page is loaded, the page will not echo out the members informations but if i removed the IF function, all the members information will be display but the form will be submitting itself automatically.

Code: Select all

<?php
[color=#BF0000]if(!empty($_POST[Submit]))[/color]
{
include("customersinfodb.php");
$id=$_SESSION[id];
$check=mysql_query("select * from clients where id='$id'");
while($look=mysql_fetch_array($check))
{
      $id=$look[id];
      $ano=$look[ano];
      $fname=$look[fname];
      $mname=$look[mname];
      $lname=$look[lname];
      $address=$look[address];
      $address2=$look[address2];
      $city=$look[city];
      $state=$look[state];
      $country=$look[country];
      $pcode=$look[pcode];
      $gender=$look[gender];
      $pno=$look[pno];
      $pno2=$look[pno2];
      $atype=$look[atype];
      $currency=$look[currency];
      $status=$look[status];
      }
      $nfname=$_POST[fname];
      $nmname=$_POST[mname];
      $nlname=$_POST[lname];
      $naddress=$_POST[address];
      $naddress2=$_POST[address2];
      $ncity=$_POST[city];
      $nstate=$_POST[state];
      $ncountry=$_POST[country];
      $npcode=$_POST[pcode];
      $ngender=$_POST[gender];
      $npno=$_POST[pno];
      $npno2=$_POST[pno2];
      $natype=$_POST[atype];
      $ncurrency=$_POST[currency];
      $nstatus=$_POST[status];
      $update=mysql_query("update clients set fname='$nfname', mname='$nmname', lname='$nlname', address='$naddress', address2='$naddress2', city='$ncity', state='$nstate', country='$ncountry', pcode='$npcode', gender='$ngender', pno='$npno', pno2='$npno2', atype='$natype', currency='$ncurrency', status='$nstutas' where ano='$ano'");
      if(!$upload)
      echo "The Information is not updated, try again.<br>";
      else
      echo "The Information is updated successfully.<br>";
     }
      ?>
what can i do to this form?

Regards,
adsegzy
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: how to calculate % and how to stop form from auto submit.

Post by markusn00b »

adsegzy wrote:Hello Success-Oriented People,

I want to calculated 90% and a particular amount am having in my database. I named the column of the amount in the database abalance. this is the code i tried but its not working,

Code: Select all

$ninetypercent=(90/100*'$abalance');
What is the way out?

Secondly, am having a form where members can edit their account. When the member clicked on edit profile from his/her welcome page, the below page will be opened. my problem is that starting the form with teh IF function in order to stop the form from submitting automatically, whenever the page is loaded, the page will not echo out the members informations but if i removed the IF function, all the members information will be display but the form will be submitting itself automatically.

Code: Select all

<?php
[color=#BF0000]if(!empty($_POST[Submit]))[/color]
{
include("customersinfodb.php");
$id=$_SESSION[id];
$check=mysql_query("select * from clients where id='$id'");
while($look=mysql_fetch_array($check))
{
      $id=$look[id];
      $ano=$look[ano];
      $fname=$look[fname];
      $mname=$look[mname];
      $lname=$look[lname];
      $address=$look[address];
      $address2=$look[address2];
      $city=$look[city];
      $state=$look[state];
      $country=$look[country];
      $pcode=$look[pcode];
      $gender=$look[gender];
      $pno=$look[pno];
      $pno2=$look[pno2];
      $atype=$look[atype];
      $currency=$look[currency];
      $status=$look[status];
      }
      $nfname=$_POST[fname];
      $nmname=$_POST[mname];
      $nlname=$_POST[lname];
      $naddress=$_POST[address];
      $naddress2=$_POST[address2];
      $ncity=$_POST[city];
      $nstate=$_POST[state];
      $ncountry=$_POST[country];
      $npcode=$_POST[pcode];
      $ngender=$_POST[gender];
      $npno=$_POST[pno];
      $npno2=$_POST[pno2];
      $natype=$_POST[atype];
      $ncurrency=$_POST[currency];
      $nstatus=$_POST[status];
      $update=mysql_query("update clients set fname='$nfname', mname='$nmname', lname='$nlname', address='$naddress', address2='$naddress2', city='$ncity', state='$nstate', country='$ncountry', pcode='$npcode', gender='$ngender', pno='$npno', pno2='$npno2', atype='$natype', currency='$ncurrency', status='$nstutas' where ano='$ano'");
      if(!$upload)
      echo "The Information is not updated, try again.<br>";
      else
      echo "The Information is updated successfully.<br>";
     }
      ?>
what can i do to this form?

Regards,
adsegzy
To your first problem. Single quoted strings are not parsed for variables. Either use double quotes, or, preferably, omit the quotes altogether, so your expression becomes: $ninetypercent=(90/100 * $abalance); - A little spacing never hurt anyone ;)

To your second problem: I'm having a hard time understanding. What do you mean 'the page will submit itself automatically'? Also, you are not surrounding your array indexes with quotes (single or double). This is bad practice and should be avoided!
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

Re: how to calculate % and how to stop form from auto submit.

Post by adsegzy »

Hello markusn00b, what is mean by auto submit is that; if the php script is not in the form, whenever the page is opened it will not submit, but with php script in the form, anytime that the page is opened it will trigger the submit bottom automatically except if i put the "if(!empty($_POST[submit]))", that is why i put it in red.
So the problem is if i put the "if" function, the "edit_members_profile.php" will not echo out the member's information but if i do not put the "if" function, the page will echo out the member's information and try to submit is automatically without the member clicking the submit bottom. I hope you are cleared now.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: how to calculate % and how to stop form from auto submit.

Post by markusn00b »

Still not getting you totally.. I guess this is a language barrier.

Anyway, let me try and sum up my (possible lack of) understanding: you want to prevent the PHP script from being executed unless it has arrived from a form submission, which is why you check for $_POST['Submit']? If you are submitting a form to this page, then what is the problem? If you have a form element with the attribute name='Submit', then this will be present in the POST array and the IF code block will execute.

Mark.
Post Reply