Begyndelsen på en indkøbskurv - vha. Smarty/MVC
Advarsel: Denne er lang!NB!: Der er endnu ikke lavet en kalkulation, den vælger jeg lige at se bort fra i starten.
Håber, der sidder en Smarty/php ekspert derude.
Jeg har en side med en række varer og en indkøbskurv, hvor man med et klik bestiller en vare og henter den ind på template-filen indkoebskurv.tpl med (bestilling.php?indkoebskurv=72)
Vælger jeg bestil i formularen på templaten, udskrives følgende:
Notice: Undefined index: title in /x/bestilling.php on line 13
Notice: Undefined index: author in /x/bestilling.php on line 13
Notice: Undefined index: publisher in /x/bestilling.php on line 13
Notice: Undefined index: year in /x/bestilling.php on line 13
Notice: Undefined index: price in /home/x/bestilling.php on line 13
Warning: Cannot modify header information - headers already sent by (output started at /home/x/bestilling.php:13) in /home/x/bestilling.php on line 14
?????
Min bestilling.php ser således ud:
<?php
include('setup.php');
include('booksKoeber.inc.php');
mysql_connect(x) or die ("Der kunne ikke oprettes forbindelse til databasen");
mysql_select_db(x);
if
(is_array($_POST) && isset($_POST['bestil']) && isset($_POST['id'])) {
indkoebskurv($_POST['id'], $_POST['title'], $_POST['author'], $_POST['publisher'], $_POST['year'], $_POST['price']);
header("Location: bestilling.php");
}elseif
(is_array($_GET) && isset($_GET['indkoebskurv'])) {
$row=hentBog($_GET['indkoebskurv']);
$smarty->assign('row',$row);
$smarty->display('indkoebskurv.tpl');
}else{
$rows=hentAlleBoeger();
$smarty->assign('rows',$rows);
$smarty->display('bestilling.tpl');
}
?>
Min booksKoeber.inc.php:
<?php
function hentBog($id) {
$sql="SELECT * FROM books WHERE id=$id";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
mysql_free_result($result);
return $row;
}
function hentAlleBoeger() {
$query = mysql_query("SELECT * FROM books order by author");
while($row = mysql_fetch_array($query)) {
$rows[]=$row;
}
mysql_free_result($query);
return $rows;
}
function indkoebskurv($id, $title, $author, $publisher, $year, $price) {
$sql="INSERT INTO indkoebskurv author='$author', title='$title',
publisher='$publisher', year='$year', price='$price'";
$result=mysql_query($sql)or die (mysql_error());
return;
}
Min indkoebskurv.tpl ser således ud (undskylder alt det overflødige):
<form onSubmit="return valider(this)" name="indkoebskurv" action="bestilling.php" method="post">
<input type="hidden" name="id" value='{$row.id}'>
<input type="hidden" name="bestil" value="ok">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="34%" class="undermenu">Title:</td>
<td width="66%" class="undermenu">{$row.title}</td>
</tr>
<tr>
<td width="34%" class="undermenu">Author:</td>
<td width="66%" class="undermenu">{$row.author}</td>
</tr>
<tr>
<td width="34%" class="undermenu">Publisher:</td>
<td width="66%" class="undermenu">{$row.publisher}</td>
</tr>
<tr>
<td width="34%" class="undermenu">Year:</td>
<td width="66%" class="undermenu">{$row.year}</td>
</tr>
<tr>
<td width="34%" class="undermenu">Price:</td>
<td width="66%" class="undermenu">{$row.price}</td>
<td width="34%" class="undermenu">Quantity:</td>
<td width="66%" class="undermenu"><input type="text" name="quantity" size="30" style="height:16px; width:82px; border:1px solid black" class="tekst" value="quantity" onFocus="if(this.value=='quantity')this.value='';" onBlur="if(this.value=='')this.value='quantity';this.document.form1.send_knap.blur()"></td>
</tr>
<tr>
<td width="34%"> </td>
<td width="66%">
</td>
</tr>
</table>
<br>
<input type="text" name="fornavn" size="30" style="height:16px; width:140px; border:1px solid black" class="tekst" value="Dit fornavn" onFocus="if(this.value=='Dit fornavn')this.value='';" onBlur="if(this.value=='')this.value='Dit fornavn';this.document.form1.send_knap.blur()">
<input type="text" name="efternavn" size="30" style="height:16px; width:140px; border:1px solid black" class="tekst" value="Dit efternavn" onFocus="if(this.value=='Dit efternavn')this.value='';" onBlur="if(this.value=='')this.value='Dit efternavn';this.document.form1.send_knap.blur()">
<input type="text" name="adresse" size="30" style="height:16px; width:140px; border:1px solid black" class="tekst" value="Din adresse" onFocus="if(this.value=='Din adresse')this.value='';" onBlur="if(this.value=='')this.value='Din adresse';this.document.form1.send_knap.blur()">
<input type="text" name="postnr" size="4" style="height:16px; width:52px; border:1px solid black" class="tekst" value="Dit postnr" onFocus="if(this.value=='Dit postnr')this.value='';" onBlur="if(this.value=='')this.value='Dit postnr';this.document.form1.send_knap.blur()">
<input type="text" name="bynavn" size="30" style="height:16px; width:82px; border:1px solid black" class="tekst" value="bynavn" onFocus="if(this.value=='bynavn')this.value='';" onBlur="if(this.value=='')this.value='bynavn';this.document.form1.send_knap.blur()">
<input type="text" name="telefonnr" size="8" style="height:16px; width:140px; border:1px solid black" class="tekst" value="Dit telefonnr" onFocus="if(this.value=='Dit telefonnr')this.value='';" onBlur="if(this.value=='')this.value='Dit telefonnr';this.document.form1.send_knap.blur()">
<input type="text" name="email" size="30" style="height:16px; width:140px; border:1px solid black" class="tekst" value="Din email" onFocus="if(this.value=='Din email')this.value='';" onBlur="if(this.value=='')this.value='Din email';this.document.form1.send_knap.blur()">
<br>
<input type="submit" name="send_knap" value="Bestil" style="height:16px; width:140px; border:1px solid black" class="tekst">
</form>
