Am o nelamurire

Secţiune dedicată începatorilor.

Moderatori: Moderatori, Start Moderator

djkps
New Member
Mesaje: 3
Membru din: Mie Feb 16, 2011 9:06 pm

Am o nelamurire

Mesajde djkps » Mie Feb 16, 2011 9:17 pm

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!



iSeLast
Junior Member
Mesaje: 42
Membru din: Mar Mar 30, 2010 2:35 pm
Localitate: Rm. Valcea
Contact:

Mesajde iSeLast » Mie Feb 16, 2011 9:31 pm

incearca asa:
[php]
<?php
if(isset($_POST['data'])) {
$data=$_POST['data'];
}
if(isset($_POST['distanta'])) {
$distanta=$_POST['distanta'];
}
?>[/php]

guppy
Senior Member
Mesaje: 353
Membru din: Joi Mai 03, 2007 12:14 am

Mesajde guppy » Mie Feb 16, 2011 9:49 pm

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;
}
?>

djkps
New Member
Mesaje: 3
Membru din: Mie Feb 16, 2011 9:06 pm

Mesajde djkps » Mie Feb 16, 2011 9:50 pm

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]

Avatar utilizator
bozghiyy
PHPRomania Supporter
Mesaje: 11
Membru din: Lun Feb 07, 2011 7:56 pm

Mesajde bozghiyy » Mie Feb 16, 2011 9:56 pm

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.

djkps
New Member
Mesaje: 3
Membru din: Mie Feb 16, 2011 9:06 pm

Mesajde djkps » Mie Feb 16, 2011 10:20 pm

Am inteles acum.
Va multumesc tuturor pentru explicatii!

perseadrian
Average Member
Mesaje: 83
Membru din: Mar Iun 22, 2010 2:39 pm

Mesajde perseadrian » Joi Feb 17, 2011 10:51 am

"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]


Înapoi la “PHP Incepători”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 27 vizitatori