Jump to content
PauLLiK

Script php care sare peste bucla elseif!

Recommended Posts

Salut, sunt nou pe RST si intr-ale programarii!

Invat php si am ceva probleme la if-uri!

This is the html:

<html>
<head>
<title>Piese auto</title>
</head>
<body>
<h1>Piese auto</h1>
<h2>Introduceti comanda</h2>
<form action="processorder.php" method=post>
<table border="0">
<tr bgcolor=#cccccc>
<td width="150">Piesa</td>
<td>Pret</td>
<td width="15">Cantitate</td>
</tr>

<tr>
<td>Anvelope</td>
<td><center>$100</center></td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3" />bucati</td>
</tr>

<tr>
<td>Sticle de ulei</td>
<td><center>$10</center></td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3" />bucati</td>
</tr>

<tr>
<td>Bujii</td>
<td><center>$4</center></td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3" />bucati</td>
</tr>

<tr>
<td colspan="2" align="center"><input type="submit" value="Introdu comanda" /></td>
</tr>
</table>
</form>
<h3>Valoare TVA: 19%</h3>
</body>
</html>

And this...php file:

<html>
<head>
<title>Piese auto - Comanda introdusa</title>
</head>
<body>
<h1>Piese auto</h1>
<h2>Comanda introdusa</h2>
<?php
echo'<p>Comanda procesata la ';
echo date('H:i, jS F');
echo '<p>';

$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
if($totalqty == 0)
{
echo 'Nu ai comandat nimic<br/>';
}
elseif(is_numeric($tireqty) && is_numeric($oilqty) && is_numeric($sparkqty) && is_int($tireqty) && is_int($oilqty) && is_int($sparkqty))
{
echo '<p>Ai comandat urmatoarele:</p>';

if($tireqty > 0)
{
echo $tireqty. ' anvelope<br />';
}
else
{
echo 'Nu ai comandat anvelope!<br />';
}
if($oilqty > 0)
{
echo $oilqty. ' sticle de ulei<br />';
}
else
{
echo 'Nu ai comandat ulei!<br />';
}
if($oilqty > 0)
{
echo $sparkqty. ' bujii<br />';
}
else
{
echo 'Nu ai comandat bujii!<br />';
}
}
else
{
echo 'Trebuie sa introduci <u>doar numere intregi</u>(ex.: 3, 6, 14)<br />';
}

/* echo 'Total piese: ' .$totalqty.'<br />';
echo '<br />';
echo '<p>Obiecte comandate: </p>';
echo $tireqty. ' anvelope;<br />';
echo $oilqty. ' sticle de ulei;<br />';
echo $sparkqty. ' bujii;<br />';*/

$totalamount = 0.00;

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE;

echo 'Subtotal: $' .number_format($totalamount, 2). '<br />';
echo '<br />';

$taxrate = 0.19;
$totalamount = $totalamount * (1 + $taxrate);

echo 'Total(TVA inclus): $' .number_format($totalamount, 2);
?>
</body>
</html>

Tocmai ce am scapat de o eroare strasanta(Parse error: syntax error, unexpected T_ELSE in....), acum am rezolvat, dar indiferent de ce as scrie in campurile din fisierul html, scriptul php imi sare peste elseif-ul care ar trebui sa verifice is_int, is_numeric si $totalqty > 0, $oilqty > 0...., deci sare peste el si trece direct la else, in concluzie, imi afiseaza(indiferent ce as completa in forumular) "Trebuie sa introduci doar numere intregi (ex.: 3, 6, 14) si cu totalul si subtotalul...

Nu stiu cum sa-l fac sa intre in bucla de la elseif(sau care e problema) ca sa verifice conditiile alea...si apoi pe cele din if-urile incluse in bucla lui elseif!

Scuzele mele daca m-am explicat ambiguu!

Multumesc!

Link to comment
Share on other sites

Deci in pagina html eu introduc niste date, daca nu am scris nimic si apas butonul de submit scriptul php ar trebui sa-mi afiseze ca nu am introdus nimic!

In caz in care introduc ceva scriptul trebuie sa verifice daca datele sunt numere intregi, daca sunt atunci afiseaza ce piese am comandat si ce piese nu am comandat, iar daca datele nu sunt numere intregi atunci ar trebui sa-mi afiseze ca trebuie sa recompletez formularul html!

Ce nu merge?

Indiferent de ce as completa in formular scriptul imi spune ca ar trebui sa introduc numere intregi, deci sare peste verificarea cu is_numeric, is_int.

Aici e problema, el zice asta si daca eu completez corect toate campurile!

Sper ca ati inteles...

Link to comment
Share on other sites

pai tu in acel elseif verifici is_int ... nu o sa iti intre niciodata in acea conditie daca tu preiei direct valorile din $_POST. Prin $_POST toate valorile sunt string! daca chiar vrei poti sa faci un cast to int.

exemplu:

$tireqty = (int) $_POST['tireqty'];

you are right :)

Link to comment
Share on other sites

if (eregi([0-9], (int) $_POST['tireqty'])) ....

sau

if (preg_match('/^\d$/', (int) $_POST['tireqty']))

exemplu:

$is_varsta = ereg('^[0-9]+$',$varsta);

if($is_varsta == false)

{

echo "<font color='red'>Varsta incorecta: $varsta<br/></font>";

}

In plus iti recomand sa folosesti isset($_POST['Submit']).

Succes.

Link to comment
Share on other sites

if (eregi([0-9], (int) $_POST['tireqty'])) ....

sau

if (preg_match('/^\d$/', (int) $_POST['tireqty']))

exemplu:

$is_varsta = ereg('^[0-9]+$',$varsta);

if($is_varsta == false)

{

echo "<font color='red'>Varsta incorecta: $varsta<br/></font>";

}

In plus iti recomand sa folosesti isset($_POST['Submit']).

Succes.

Imi poti explica ce ai zis pe acolo?

Sunt incepator si nu stiu prea multe, abia ce sunt la cap2 al cartii din care invat!

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...