Jump to content

Javascript functions in PHP ?


BoNfiRe

Recommended Posts

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:
[code]
<?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";
?>
[/code]

Any help would be much appreciated.
ty
BoN
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--quoteo(post=384449:date=Jun 16 2006, 04:30 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 16 2006, 04:30 AM) [snapback]384449[/snapback][/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
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.