SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member davestott1's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problems Using Javascript with PHP

    I have the following code on a page I am working on:

    <script language="JavaScript">
    function discounter(oForm)

    {

    if (isNaN(oForm.itemprice.value))

    {

    alert('Please enter a valid price.');

    oForm.itemprice.focus();

    oForm.itemprice.select();

    return false;

    }

    else

    {

    oForm.total.value = oForm.itemprice.value / 100 * 10;

    oForm.discountedprice.value = (oForm.itemprice.value - oForm.total.value);

    }

    }



    function $format(num)

    {

    var $str = String(Math.round(num * 100) / 100);

    var hasdot = ($str.indexOf('.') != -1);

    $str = (hasdot) ? $str + '00' : $str + '.00';

    return '' + $str.substring(0, $str.indexOf('.') + 3);

    }



    </script>

    The page contains some PHP which is causing me problems with the bit from function $format onwards. Basically, the JS is failing (I think this is because the $ signs are being interpreted as PHP)

    can anyone offer me any advice on how to get around this?

    Thanks for any help you can provide

  2. #2
    SitePoint Member mattvg's Avatar
    Join Date
    Nov 2003
    Location
    Goleta, CA
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a reason you need '$'s in the JavaScript (I'd just get rid of them as a first move)?

  3. #3
    SitePoint Member davestott1's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattvg
    Is there a reason you need '$'s in the JavaScript (I'd just get rid of them as a first move)?
    The $ signs are need for the function to work properly

    Removing them stops the Javascript working altogether

    It seems to be that because PHP uses the $ sign it is interpreting that $ signs in the JS as being PHP

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not necessarily - it depends on how your code is structured.

    Anyway, $ is just an acceptable variable character in JS, unnecessary for something to work - just used them to make the purpose of the function ($[dollar]format) clearer.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript">
    
    function discounter(oForm)
    {
    	var tempval = oForm.itemprice.value.replace(/\$/, '');
    	if (isNaN(tempval))
    	{
    		alert('Please enter a valid price.');
    		oForm.itemprice.focus();
    		oForm.itemprice.select();
    		return false;
    	}
    	else
    	{
    		oForm.total.value = tempval / 100 * 10;
    		oForm.discountedprice.value = curr_format(tempval - oForm.total.value, ' $');
    	}
    } 
    
    function curr_format(num, prefix)
    {
    	var sTemp = String(Math.round(num * 100) / 100);
    	var hasdot = (sTemp.indexOf('.') != -1);
    	sTemp = (hasdot) ? sTemp + '00' : sTemp + '.00';
    	return prefix + sTemp.substring(0, sTemp.indexOf('.') + 3);
    }
     
    </script>
    </head>
    
    <body>
    <form name="form1" method="post" action="" onreset="itemprice.focus()">
    <input name="itemprice" type="text" onblur="return discounter(this.form)" id="itemprice" size="8" />______<b>Item Price</b>
    <br /><br />
    <input name="discountedprice" type="text" readonly="readonly" size="8" />______<b>With Discounted Price</b>
    <input name="total" type="hidden" id="total" />
    <br /><br />
    <input type="reset" />
    </form>
    </body>
    </html>
    ::: certified wild guess :::


Bookmarks

Posting Permissions

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