3

why am I getting undefined index's with my form is it because of the encoding type I am using, if so what can I do to fix this to properly post my variables

<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post"                action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
                    <option disabled selected>select one...</option>
                    <option value="Algoma">Algoma University</option>
                    <option value="york">York University</option>
                 </select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php echo $_POST["mysex"]; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php echo $_POST["lookingfor"]; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form>   

I know that the variables being posted from say page1 to this form are coming through because I have an if statement with an isset() for the variables making it header to another page if there not set. this form code is from page2

im using this code on page3 to recieve the form data

$mysex = $_POST['mysex'];
$lookingfor = $_POST['lookingfor'];
$uni = $_POST['university'];

So when I post all the variable from this form to another page I get

Notice: Undefined index: mysex in C:\xampp\htdocs\Website\personalspage.php on line 4

Notice: Undefined index: lookingfor in C:\xampp\htdocs\Website\personalspage.php on line 5

Notice: Undefined index: university in C:\xampp\htdocs\Website\personalspage.php on line 6

I double checked and made sure that all my methods are using post, the only thing I can think of why this isnt working is because of some sort of combination of echoing input values and the enctype. If anyone could help me out it would be greatly appreciated.

Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206
  • whats the output of this print_r($_POST) – shamon shamsudeen Jul 13 '15 at 04:23
  • any possibility that you are using javascript to submit the form? you have `onfocus="emptyElement('status')"` in your select, any chance that is impacting your code? – Sean Jul 13 '15 at 04:42
  • @Sean the onfocus function shouldnt be there meant to delete it, the value for uuniveristy goes through its the "mysex" and "value" that arent going through which i think might have to do with echoing the value – Jeremy Sills Jul 13 '15 at 05:20
  • @shammon output is nothing what im doing is from page1 i post values to page2 and take those values add a few new values and post them back to page1 put print_r($_POST) is not showing anything after I go back to page1 – Jeremy Sills Jul 13 '15 at 05:22
  • There could be a bug somewhere. Does `var_dump(file_get_contents('php://input'));` or `var_dump($_POST);` show anything interesting? What webserver (and version) are you running? Which version of PHP? – Scott Arciszewski Jul 14 '15 at 22:41

2 Answers2

0

Undefined variable university means you didnt select any options to fix that you can set any of the ptions to selected and for hidden variables problem is value is not there. fixed code is here

<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post"                action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
                    <option disabled selected value="" selected>select one...</option>
                    <option value="Algoma">Algoma University</option>
                    <option value="york">York University</option>
                 </select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php if(isset($_POST["mysex"])) echo $_POST["mysex"];else echo ""; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php if(isset($_POST["lookingfor"]))echo $_POST["lookingfor"];else echo ""; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form> 
Dhinju Divakaran
  • 893
  • 1
  • 8
  • 11
  • 1
    do you care to share why the OP should `Try this code`? – Sean Jul 13 '15 at 04:33
  • @Dhinju Divakaran if the variables are posted what is need for checking if they are set, I know 100% they are posted to the page with the form – Jeremy Sills Jul 13 '15 at 05:30
  • @Sean i dont understnad what you mean by do you care to share why the OP should Try this code? – Jeremy Sills Jul 13 '15 at 05:43
  • @JeremySills when this answer was first posted it was just `Try this code` and the block of code, which was a vague answer. see http://stackoverflow.com/posts/31375457/revisions I was trying to encourage the poster to share why their code block was an answer for your question. – Sean Jul 13 '15 at 13:54
  • @Dhinju Divakaran yes i tried the code it still not working – Jeremy Sills Jul 13 '15 at 18:45
0

I Have Any Two option
First:

error_reporting(0);

Second:

<input type="hidden" name="mysex" id="mysex" 
value="<?php if(isset($_POST["mysex"])){ echo $_POST["mysex"]; }?>">
<input type="hidden" name="lookingfor" id="lookingfor" 
value="<?php if(isset($_POST["lookingfor"])){ echo $_POST["lookingfor"]; }?>">
Sagar Patel
  • 394
  • 1
  • 2
  • 11