hello i have a problem that's maybe difficult to descripe,
i have an application, that simulates a pizza store. i have a page "bestellungen" ("orders") in which i have some javascript.(the head,javascript and body i declare in page.php, from which the other ("sub")pages "extends".
right now, everything works, but now, i want to add to the javascript some php code. (in the "main" page.
in the page "bestellungen" ("orders") i read from the database
$sql="Select * FROM pizzen_arten";
$data=$this->_database->query($sql); //alle daten in data gespeichert
while($reihe = $data->fetch_array()){
$lfdnr=$reihe[2];
$this->pizza_name[$lfdnr] =$reihe[0];
$this->pizza_preis[$lfdnr] =$reihe[1];
$this->pizza_id[$lfdnr] =$reihe[2];
}
then in the same page i add all pizzas
foreach ($this->pizza_id as $tmp) //alle pizzen durchgehen über id, (id in tmp speichern)
{
//echo " überall wo " ist ein \ davor
echo "<img src=\"pizzen/pizza-".$this->pizza_name[$tmp].".png\" width=\"50\" height=\"50\" alt=\"Pizza ".$this->pizza_name[$tmp]."\" id=\"".$this->pizza_name[$tmp]."\" onclick=\"hinzu('".$this->pizza_name[$tmp]."')\" /> ".$this->pizza_name[$tmp]." ".$this->pizza_preis[$tmp]." €";
echo "<p/>"; //vor variable " string beenden . für "+" dann var, dann string weiter
//this das aktuelle obj, der schleife, davon pizzaname (ist ein array! kein attribut), an der stelle tmp
}
the method "hinzu(id)" is declared in "page.php"
function hinzu (pizza)
{
NeuerEintrag = new Option(pizza, pizza, false, false);
document.getElementById("warenkorbfeld").options[document.getElementById("warenkorbfeld").length] = NeuerEintrag ;
ETO;
foreach ($this->pizza_id as $tmp)
{
if ($this->pizza_name[tmp]==pizza) //array durchgehen gucken, wann der übergabe parameter ==einem pizzanamen
echo "gesamtbetrag=gesamtbetrag"+$this->pizza_preis[$tmp];
}
/*if (pizza=="Margherita")
{
gesamtbetrag = gesamtbetrag + 4;
}
if (pizza=="Salami")
{
gesamtbetrag = gesamtbetrag + 4.50;
}
if (pizza=="Hawaii")
{
gesamtbetrag = gesamtbetrag + 5.50;
}*/
echo <<< ETO
document.开发者_运维问答getElementById('gesamtbetrag').innerHTML=gesamtbetrag ;
}
at first the method was without php-code, now by using php-code it doesn't work.
i suggest there might be a problem with "$this" or "pizza" but i'm not quite sure wether i implemented the php code right.
thanks in advance
You can't simply mixup JavaScript and PHP code Like this. JavaScript Code is executed in the Users Browser and the PHP code is executed on your server. So you have to make clear which code is to be executed on you server. In PHP you do this with the PHP Tags <?php
& ?>
:
function hinzu(pizza) {
NeuerEintrag = new Option(pizza, pizza, false, false);
document.getElementById("warenkorbfeld").options[document.getElementById("warenkorbfeld").length] = NeuerEintrag ;
<?php foreach ($this->pizza_id as $tmp): ?>
gesamtbetrag=gesamtbetrag + <?php= $this->pizza_preis[$tmp] ?>;
<?php endforeach; ?>
document.getElementById('gesamtbetrag').innerHTML = gesamtbetrag;
}
or if I'm interpreting your heredoc strings correctly:
echo <<<ETO
function hinzu(pizza) {
NeuerEintrag = new Option(pizza, pizza, false, false);
document.getElementById("warenkorbfeld").options[document.getElementById("warenkorbfeld").length] = NeuerEintrag ;
ETO;
foreach ($this->pizza_id as $tmp) {
echo "gesamtbetrag=gesamtbetrag+" . $this=>pizza_preis[$tmp];
// The important differnces: ^ ^
}
echo <<<ETO
document.getElementById('gesamtbetrag').innerHTML = gesamtbetrag;
}
ETO;
精彩评论