De cateva zile incerc si eu sa invat PHP.
Am dat peste o eroare care imi mari batai de cap pentru ca nu reusesc sa gasesc rezolvarea.
Am urmatorul exemplu de cod si ma intereseaza cum scap de erorile de "Undefined index":
<html>
<head>
<title>Proiect 4</title>
</head>
<body>
<?php
$data=$_POST["data"];
$distanta=$_POST["distanta"];
?>
<form method="post" action="4.php">
<table>
<tr>
Data:<br />
<td><input type="text" name="data" value="<?php echo $data; ?>" /></td>
</tr>
<tr>
<td align="center">+</td>
</tr>
<tr>
<td><input type="text" name="distanta" value="<?php echo $distanta; ?>" /></td>
</tr>
<tr>
<td align="center"><input type="submit" value="=" /></td>
</tr>
<tr>
<td><input type="text" name="rezultat" value="<?php echo date("M j, Y", (strtotime($data)+60*60*24*($distanta))); ?>" disabled="disabled" /></td>
</tr>
</table>
</form>
</body>
</html>
Va multumesc anticipat!
Am o nelamurire
Moderatori: Moderatori, Start Moderator
Cod: Selectaţi tot
<input type="submit" value="=" name="adunare" />si apoi :
Cod: Selectaţi tot
<?php
if (isset($_POST["adunare"])){
$data=$_POST["data"];
$distanta=$_POST["distanta"];
if (emty($data)) {
echo "campul data necompletat"
}else if (emty($distanta)) {
echo "campul distanta necompletat"
}esle {
$rezultat = $data+$distanta;
echo "rezultatul este: ".$rezultat;
}
?> Daca las asa oricum primesc eroarea in campuri dar am introdus cate un "else" acolo cu valoare pe care sa o aiba inainte de a primi valoare si este ok acum.
Iti mutumesc! M-ai scos din ceata!
Am folosit asa:
<?php
if(isset($_POST['data'])) {
$data=$_POST['data'];
}
else $data="";
if(isset($_POST['distanta'])) {
$distanta=$_POST['distanta'];
}
else $distanta="";
?>[/list][/list]
Iti mutumesc! M-ai scos din ceata!
Am folosit asa:
<?php
if(isset($_POST['data'])) {
$data=$_POST['data'];
}
else $data="";
if(isset($_POST['distanta'])) {
$distanta=$_POST['distanta'];
}
else $distanta="";
?>[/list][/list]
Explicatia este ca $_POST nu are atribuita nici o valoare pana nu trimiti un formular.
Astfel variabila $distanta = $_POST[ un index care nu exista pana nu trimiti formularul ];
Ca sa rezolvi problema trebuie sa verifici ca formularul a fost trimis , sau ca variabilele exista. Pentru asta exemplele de mai sus sunt bune.
Astfel variabila $distanta = $_POST[ un index care nu exista pana nu trimiti formularul ];
Ca sa rezolvi problema trebuie sa verifici ca formularul a fost trimis , sau ca variabilele exista. Pentru asta exemplele de mai sus sunt bune.
-
perseadrian
- Average Member
- Mesaje: 83
- Membru din: Mar Iun 22, 2010 2:39 pm
"It is not necessary to initialize variables in PHP however it is a very good practice."
sursa:http://www.php.net/manual/en/language.variables.basics.php
Ca sa rezolvi problema:
Deschizi php.ini si editezi sectiunea de mai jos cum doresti:
Common Values:
; E_ALL & ~E_NOTICE (Show all errors, except for notices and coding standards warnings.)
; E_ALL & ~E_NOTICE | E_STRICT (Show all errors, except for notices)
; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)
; E_ALL | E_STRICT (Show all errors, warnings and notices including coding standards.)
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_NOTICE
De obicei pe toate serverele de hosting este ceva de genu:E_ALL & ~E_NOTICE , si nu afiseaza warning-urile.
Sper ca te ajuta.
[/b]
sursa:http://www.php.net/manual/en/language.variables.basics.php
Ca sa rezolvi problema:
Deschizi php.ini si editezi sectiunea de mai jos cum doresti:
Common Values:
; E_ALL & ~E_NOTICE (Show all errors, except for notices and coding standards warnings.)
; E_ALL & ~E_NOTICE | E_STRICT (Show all errors, except for notices)
; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)
; E_ALL | E_STRICT (Show all errors, warnings and notices including coding standards.)
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_NOTICE
De obicei pe toate serverele de hosting este ceva de genu:E_ALL & ~E_NOTICE , si nu afiseaza warning-urile.
Sper ca te ajuta.
[/b]
Cine este conectat
Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 27 vizitatori
