Link to home
Start Free TrialLog in
Avatar of Charles Lamarche
Charles Lamarche

asked on

Why is $_POST returning empty string ?

I am trying to retrieve user input from a form in my PHP web app so that i can assign these values to variables with the $_POST[' '] command. Then, i want to make changes to a database with the values retrieved. But my problem is that the $_POST[' '] command is returning empty values for every field i put information in..
To put into context, i am using Oracle VM VirtualBox with a Linux VM, and within this i use the Eclipse Workspace to code my program. Here is the php file with the code that is not working:

<?php 
require "init.php";

if(empty($_SERVER['CONTENT_TYPE'])) {
    $SERVER['CONTENT_TYPE'] = "application/x-www-form-urlencoded";
}

if (!isset($_SESSION['NOM_UTILISATEUR'])) {
    echo "<script>alert('Vous n'êtes pas actuellement connecté ! Redirection vers la page de connexion ...');
    window.location='index.php'</script>";
    session_destroy();
}

$_POST['SELECT_ENCAN'] = "";
$_POST['DESCRIPTION'] = "";
$_POST['TITRE'] = "";
$_POST['DONATEUR_MAJ'] = "";
$_POST['VALEUR'] = "";
$_POST['PRIX_INITIAL'] = "";
$_POST['INCR_MIN'] = "";
$_POST['ACH_IMM'] = "";
$_POST['PHOTO'] = "";

$select_encan = "";
$titre = "";
$description = "";
$donateur_maj = "";
$valeur = "";
$prix_ini = "";
$incr_min = "";
$ach_imm = "";
$chemin_photo = "";

$page_ajout = $_SESSION['Ajouter'];
$page_maj = $_SESSION['MAJ'];

if ($page_ajout == true) {
    if (isset($_POST['OK_MAJ'])) {
      
        $titre = $_POST['TITRE'];
        $description = $_POST['DESCRIPTION'];
        $donateur_maj = $_POST['DONATEUR_MAJ'];
        $valeur = $_POST['VALEUR'];
        $prix_ini = $_POST['PRIX_INITIAL'];
        $incr_min = $_POST['INCR_MIN'];
        $ach_imm = $_POST['ACH_IMM'];
        $chemin_photo = $_POST['PHOTO'];
        
        $query_ajout = "insert into TP2_ITEM values(NO_ITEM_SEQ.nextval, " .$select_encan. ", NO_ITEM_ENCAN_SEQ.nextval, " .$titre. ", " .$chemin_photo. ", " .$description. ", " .$donateur_maj. ", " .$valeur. ", " .$prix_ini. 
                        ", " .$incr_min. ", " .$ach_imm. ", 0, 0)";
       
        $stid = oci_parse($conn, $query_ajout);
       oci_execute($stid);
       $page_ajout = "";
       echo "<script>alert('Ajout de votre item s'est réalisé avec succès.');
       windows.location='page_principale.php'</script>";     
    }
}

?>

<html>
<head>
</head>
<body>
	<?php if ($page_ajout == true): ?>
		<form method="POST" action="page_ajout_maj.php">		
    		<p>
    		Numéro unique :<input type="text" name="NO_ITEM" value="Assignation automatique" disabled="disabled"><br>
    		
    		Encan :<?php 
        			   $tmp = "";
        			   $compteur = 0;
    			
        			   echo "<select name='SELECT_ENCAN'>";
        			   $query_encan = "select distinct NOM_ENC, DATE_DEBUT_ENC from TP2_ENCAN";
    			 	   $stid = oci_parse($conn, $query_encan);
    			 	   oci_execute($stid);
    			 	   
    			 	   while (($row = oci_fetch_array($stid, OCI_ASSOC)) != false) {
    			 	       if ($compteur == 2) {
    			 	           $tmp = "";
    			 	           $compteur = 0;
    			 	       } 
    			 	       foreach ($row as $item) {
    			 	           if ($tmp == "") {
    			 	               $tmp = $item;
    			 	           } else {
    			 	               $tmp = $tmp. " " .$item;
    			 	           }
    			 	           $compteur++;
    			 	       }  
    			 	       echo "<option value='$row'>" .$tmp. "</option>";
    			 	   }
    			 	   echo "</select><br>";
    			 	   $choix_encan = $_POST['SELECT_ENCAN'];
    			 	   oci_free_statement($stid);		
    			 	   $query_select_encan = "select NO_ENCAN from TP2_ENCAN where NOM_ENC like '%" .$choix_encan. "%' and 
                                              DATE_DEBUT_ENC like '%" .$choix_encan. "%'";
    			 	   $stid = oci_parse($conn, $query_select_encan);
    			 	   oci_execute($stid);
    			 	   $select_encan = oci_fetch_array($stid, OCI_ASSOC);
    			 	   oci_free_statement($stid);
    			  ?>
    		
    		Titre :<input type="text" name="TITRE" value="<?php echo $titre;?>"><br>
    		
    		Donateur :<input type="text" name="DONATEUR_MAJ" value="<?php echo $donateur_maj;?>"><br>
    		
    		Description :<br>
    		<textarea name="DESCRIPTION" rows="4" cols="50"><?php echo $description;?></textarea><br>                                                     
    		
    		Valeur :<input type="number" name="VALEUR" value="<?php echo $valeur;?>"><br>
    		
    		Prix départ :<input type="number" name="PRIX_INITIAL" value="<?php echo $prix_ini;?>"><br>
    		
    		Incrément :<input type="number" name="INCR_MIN" value="<?php echo $incr_min;?>"><br>
    		
    		Achat immédiat :<input type="number" name="ACH_IMM" value="<?php echo $ach_imm;?>"><br>
    		
    		Photo :<input type="text" name="PHOTO" value="<?php echo $chemin_photo;?>"><br>
    		</p>   
        	<input type="submit" name="OK_MAJ" value="OK"><input type="submit" name="Annuler_MAJ" value="Annuler">
    	</form>
    <?php endif; ?>
    <?php oci_close($conn); ?>
</body>
</html>

***The init.php file attached at the start :***

<?php
$conn = oci_connect("C##XXXXXXX", "bdXXXXXXXXX", "ift-p-ora12c.fsg.ulaval.ca:9999/XXXX12X");

session_start();

$bouton_ok = "";
$bouton_annuler = "";
$where_recherche = "";

?>

Open in new window

Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

Is this stopping the page:
 var_dump($_POST['TITRE']);
        die();

can you verify:
if ($page_ajout == true) { is working
 if (isset($_POST['OK_MAJ'])) { is working

Also, put PHP error reporting at the top of the page to see what it prints out.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Avatar of Charles Lamarche
Charles Lamarche

ASKER

yes the var_dump is stopping the page. And i will put the PHP error reporting in the code and run it to see what it prints out like you say !
So i'm trying to make an SQL query ($query_ajout) but fails to execute because the variables in the string are empty. And i don't know why the values aren't passed through the $_POST ...
i have another PHP file in my project with a form and it works just fine.
so:
$query_ajout = "insert into TP2_ITEM values(NO_ITEM_SEQ.nextval, " .$select_encan. ", NO_ITEM_ENCAN_SEQ.nextval, " .$titre. ", " .$chemin_photo. ", " .$description. ", " .$donateur_maj. ", " .$valeur. ", " .$prix_ini.
                        ", " .$incr_min. ", " .$ach_imm. ", 0, 0)";
var_dump($query_ajot); is empty?
var_dump shows this:
/var/www/html/IFT-2004_E19_TP2/page_ajout_maj.php:56:string 'insert into TP2_ITEM values(NO_ITEM_SEQ.nextval, , NO_ITEM_ENCAN_SEQ.nextval, , , , , , , , , 0, 0)' (length=99)

Open in new window

and if i put one of the variable in var_dump($titre) it gives:

/var/www/html/IFT-2004_E19_TP2/page_ajout_maj.php:45:string ' ' (length=0)
hmmm.
Take out: if ($page_ajout == true) {
    if (isset($_POST['OK_MAJ'])) {

See what happens.

 I think someone may know better than I what the issue is but I would  strip everything down to one variable at a time to find the issue and add everything back in one line at a time.
Ok i will look into this ! Thank you for your time.
You may also need to change: <?php echo $titre;?> to-
<?php if(isset($_POST['TITRE'])) {echo $titre;} ?>
i think i may be overwriting my $_POST that i submit by assigning them with = ""; at the beginning of my file like so:

$_POST['TITRE'] = "";
and so on ..

i did this because i was getting some Notices : undefined index.
And it was bugging me.
That seems right.  Comment them out.
var_dump($titre) now shows me

/var/www/html/IFT-2004_E19_TP2/page_ajout_maj.php:45:string 'something' (length=9)

that's something ! :)
comment out:
$_POST['SELECT_ENCAN'] = "";
$_POST['DESCRIPTION'] = "";
$_POST['TITRE'] = "";
$_POST['DONATEUR_MAJ'] = "";
$_POST['VALEUR'] = "";
$_POST['PRIX_INITIAL'] = "";
$_POST['INCR_MIN'] = "";
$_POST['ACH_IMM'] = "";
$_POST['PHOTO'] = "";
it works now !! but i get an undefined index notice.
User generated image
although it doesn't stop the code from running, it's just annoying.
ASKER CERTIFIED SOLUTION
Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for the help !! Have a good evening
No worries!