Jump to content


Photo

Javascript functions in PHP ?


  • Please log in to reply
5 replies to this topic

#1 BoNfiRe

BoNfiRe
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 June 2006 - 09:55 PM

Hi all.

I'm trying to add a form to my current template. But for some unknown reason it won't load the js.

If I go to the page in question directly it works fine. However, when I try it through the main site I just get js errors.

I have tried all I know and all I can get from google is to use the js as a php function but that kinda lost me and using $GET or whatever it is lost me even more [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

This is the dreded code in question:
<?php
include_once("configs/aa_config.php");

// define ('TITLE', 'Americas Army'); 
// define ('shop', '" onLoad="doPricing()" id="holdall" "');
?>

<script type="text/javascript">
function doPricing()
{
  no = 0; yes = 1;

  // work out player cost
  sPlayerValue = document.forms.form.players.options[document.forms.form.players.selectedIndex].value;
  var arrPlayerValue = sPlayerValue.split("|");
  sPlayerCost = arrPlayerValue[1];

  // work out months
  sMonthValue  = eval(document.forms.form.months.value);
  sDebrandCost = eval(document.forms.form.debranding.value)*$cost_debranding;
  sMonthCost   = (sPlayerCost * 1) + sDebrandCost;
  sTotalCost   = sMonthCost * sMonthValue;

  // set default discount
  sDiscount = 100;

<?
foreach ($arr_discounts as $key => $value)
    { echo "if (sMonthValue == $key) { sDiscount = ". (100 - $value)." }\n"; }
?>

sTotalCost   = (sTotalCost * sDiscount)/100;
totalcosteur = Math.round(sTotalCost*1.5);

document.forms.form.cost.value     = sTotalCost;
document.forms.form.total.value    = "\u00A3"+sTotalCost;
document.forms.form.totaleur.value = "\u20AC"+totalcosteur;

} 
</script>

<?
echo "<div class='ocontainer'>\n";
echo "<form name='form' method='POST' action='order.php?game=server_details'>\n";
echo "<div class='oleft'><b>Gametype:</b></div>\n";
echo "<div class='oright'>\n";
echo "    <select name='mod' onChange='doPricing()'>\n";

foreach ($arr_games as $key => $value)
    { echo "<option value='$key'>$value</option>"; }

echo "        </select>\n";
echo "    </div>\n";
echo "<div class='oleft'><b>Players:</b></div>\n";
echo "<div class='oright'>\n";
echo "    <select name='players' onChange='doPricing()'>\n";

foreach ($arr_players as $key => $value)
    { echo "<option value='$key|$value'>$key</option>"; }

echo "      </select>\n";
echo "    </div>\n";
echo "<div class='oleft'><b>Debranding: </b></div>\n";
echo "<div class='oright'>\n";
echo "      <select name='debranding' id='debranding' onChange='doPricing()'>\n";
echo "          <option value='no'>No</option>\n";
echo "          <option value='yes'>Yes</option>\n";
echo "      </select>\n";
echo "      </div>\n";
echo "<div class='oleft'><b>Months:</b></div>\n";
echo "<div class='oright'>\n";
echo "      <select name='months' onChange='doPricing()'>\n";

foreach ($arr_discounts as $key => $value)
    { echo "<option value='$key'>$key Month/s ($value"."%"." discount)</option>"; }

echo "      </select>\n";
echo "      </div>\n";
echo "<div class='oleft'><b>Payment Method:</b></div>\n";
echo "<div class='oright'>\n";
echo "      <select name='method' id='method' onChange='doPricing()'>\n";
echo "          <option value='pp'>PayPal</option>\n";
echo "      </select>\n";
echo "      </div>\n";
echo "<div class='oleft'></div>\n";
echo "    <div class='oright'>\n";
echo "    <input name=cost type=hidden>\n";
echo "    </div>\n";
echo "<div class='oleft'><b>Total:</b></div>\n";
echo "     <div class='oright'>\n";
echo "<input name=total type=text onFocus='doPricing()' size='6' maxlength='6' readonly>&nbsp;&nbsp;or&nbsp;&nbsp; <input name=totaleur type=text onFocus='doPricing()' size='6' maxlength='6' readonly>\n";
echo "     <font color=#006600>*</font>conducted in GBP (&pound;)\n";
echo "     </div>\n";
echo "<div class='oleft'></div>\n";
echo "    <div class='oright'>\n";
echo "    <input type='CHECKBOX' name='terms'> I agree to the <a href='terms.php'>Terms And Conditions</a>\n";
echo "    </div>\n";
echo "<div class='oleft'></div>\n";
echo "    <div class='oright'>\n";
echo "      <input name='button' type='submit' value='Order Now'>\n";
echo "  </div>\n";
echo "</form>\n";
echo "</div>\n";
?>

Any help would be much appreciated.
ty
BoN

#2 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 16 June 2006 - 03:25 AM

You should replace all of the " \n "'s with just " \ " because \n stands for new line.

Well,that is what I use it for, and whenever I have javascript running, I just use the \.... example:

echo "<script language=\"javascript\">
alert(\"Use the \ so you can stop the quotes.\");
<script>";



lol

-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 June 2006 - 03:30 AM

Why do you think that will help. The "\n" characters are there to make looking at the generated HTML easier. They have nothing to do with the OP's question.

As to the OP's question... I hope you realize that you PHP code will just generate the Javascript code which then gets executed after the PHP is done. Remember PHP executes on the server while Javascript executes on the client. If you want Javascript and PHP to interact, you need to use AJAX and start PHP scripts asynchrously.

Ken

#4 BoNfiRe

BoNfiRe
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 16 June 2006 - 10:33 AM

[!--quoteo(post=384449:date=Jun 16 2006, 04:30 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 16 2006, 04:30 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
As to the OP's question... I hope you realize that you PHP code will just generate the Javascript code which then gets executed after the PHP is done. Remember PHP executes on the server while Javascript executes on the client. If you want Javascript and PHP to interact, you need to use AJAX and start PHP scripts asynchrously.

Ken
[/quote]

Yes I did read that. Damn I thought there may have been a way to execute the java clientside after the php has been processed.

Ahh well anybody know any good tutorials in changing js to ajax ?

Thanks for your time
BoN

#5 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 16 June 2006 - 12:17 PM

seeing as this topics is already open and I know this is off topic, what is the deal with the term "ajax" I mean lets say you're running xhtml 1.0 with php and then have javascript functions in the page, is that automatically termed ajax?
z..z..z..z..z..z..z..z..

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 June 2006 - 02:09 PM

For the OP:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Yes I did read that. Damn I thought there may have been a way to execute the java clientside after the php has been processed.[/quote]
That is possible. I've done it in conjunction with Google Maps, Yahoo Maps, and using the Yahoo User Interface Libraries. You use PHP to write your JS code, just like you write HTML. This code will then get executed when the generated code is loaded.

AJAX is used when you want to have the JS code "call" PHP and get results back. You don't convert JS to AJAX, there are a number of functions in JavaScript that implement AJAX.

There is a sister site to this site where you can ask more questions: [a href=\"http://www.ajaxfreaks.com/\" target=\"_blank\"]Ajaxfreaks.com[/a]

Also, take a look at the [a href=\"http://developer.yahoo.com/yui/connection/\" target=\"_blank\"]Connection Manager[/a] in the [a href=\"http://developer.yahoo.com/yui/index.html\" target=\"_blank\"]Yahoo Interface Libraries[/a].

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users