0

Forgive me if I'm doing this wrong. I use this site 5 days a week to help me, but this is the first time I've actually posted a question. I'm self-taught b/c I was dumped into maintaining a php site. I know enough to be dangerous but not enough of the right way (yet). So please be kind when you feel the need to comment "WTF is this chick doing?!?!?" :)

The Gist: I have 2 files: callaway.php & saveaddcallaway.php. callaway.php connects to mysql db and checks is the user (who is already logged in) already has a record in a table called tblcallaway. If they don't, then they have a form available for them to submit a shirt order with their size, style and color. On submit, that form action passes the variables to saveaddcallaway.php which then echos the user's selections.

Really...On submit, that form action passes the variables to saveaddcallaway.php which then inserts the order into the tblcallaway and redirects the user back to callaway.php with a "Your order has been submitted" message. But for the purpose of this problem - we're just going to pretend that displaying the selections is what I need.

The Problem: It only works sometimes. You can submit and all works great. Then if you or anyone else tries to submit, it may or may not work. When it doesn’t work, you get the “Undefined Index” errors…BUT if you click the webpage refresh button a couple times, it will show your selections! It’s like the callaway.php only processes the form selections when it wants. It’s very sporadic in when it works and when it doesn’t work. I've tried different combinations of sizes/colors/styles. We’ve rebooted the server. And it’s still only working … sometimes. **Just a side note*: When it’s set to insert into tblcallaway it inserts all blanks values in the record. I've included the code, commented out, but the issue is before it gets put into the table (I think). Figured I'd include it anyways!

<!--callaway.php-->
<?php 
$query = "SELECT * FROM tblCallaway WHERE NetworkID = '$user'" or die("Error in the Select!.." . mysqli_error($link));
$result = mysqli_query($link, $query);
?>
<?php
$count = mysqli_num_rows($result);
    if ($count >= 1) {
            $greeting = "<h3>Your shirt order has been submitted - THANKS!</h3><br><br>"; 
            $hidebtn = "hidden";
            }
        else { 
            $greeting = "<h3>Submit your shirt order:</h3><br><br>"; 
            $hidebtn = "submit";
        }?>
<form method="post" action="saveadd_callaway.php" enctype="multipart/form-data">
        <?php echo $greeting;?>
  <div class="col-sm-3"><h1>STYLE:</h1><br>
            <select name="cStyle" id="cStyle">
                <option value="Mens">Mens</option>
                <option value="Ladies">Ladies</option>
            </select>
   </div> 
  <div class="col-sm-3"><h1>SIZE:</h1><br>
            <select name="cSize" id="cSize">
                <option value="S">Small</option>
                <option value="M">Medium</option>
                <option value="L">Large</option>
                <option value="XL">X-Large</option>
                <option value="2X">XX-Large</option>
                <option value="3X">XXX-Large</option>
                <option value="4X">XXXX-Large (Mens Only)</option>
            </select>
   </div> 
  <div class="col-sm-3"><h1>COLOR:</h1><br>
            <select name="cColorChoice" id="cColorChoice">
                <option value="Parakeet">Green</option>
                <option value="Black">Black</option>
                <option value="Griffen Grey">Gray</option>
                <option value="Paradise Pink">Pink-Coral</option>
                <option value="Bright White">White</option>
                <option value="Paisley Purple">Purple</option>
                <option value="Birds of Paradise Orange">Orange</option>
                <option value="Blue Atoll">Bright Blue</option>
                <option value="Riviera Blue">Riviera Blue</option>
                <option value="Peacoat Navy">Navy Blue</option>
            </select>
   </div>         
    <br style="clear: left" /><br><br><br>
    <input type="hidden" name="cNetworkID" value="<?php echo $user;?>">
    <input type="hidden" name="SubmittedOn" value="<?php echo date("Y-m-d h:i:sa");?>">
    <input type="<?php echo $hidebtn;?>" name="submit" value="submit">
    </form>

And the other...

<!--saveaddcallaway.php-->
<?php 
$cStyle = $_POST['cStyle'];
$cSize = $_POST['cSize'];
$cNetworkID = $_POST['cNetworkID'];
$ColorChoice = $_POST['cColorChoice'];
$SubmittedOn = $_POST['SubmittedOn'];

echo ($cStyle . " - " . $cSize . " - " . $ColorChoice . " - " . $cNetworkID . " - " . $SubmittedOn); 

/* *********************************************************************
$sql1 = "INSERT INTO tblCallaway (NetworkID,Style,Size,ColorChoice,SubmittedOn) VALUES ('$cNetworkID','$cStyle','$cSize','$ColorChoice','$SubmittedOn')";
if(mysqli_query($link, $sql1)){
echo "Shirt Order Successfully Submitted!";
header("Location: callaway.php"; // Redirect browser    
} else{
echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
}
********************************************************************* */

And when it decides it doesn't want to work... you get the following after submit, on the saveaddcallaway.php...

Notice: Undefined index: cStyle in E:\Inet\saveadd_callaway.php on line 10
Notice: Undefined index: cSize in E:\Inet\saveadd_callaway.php on line 11
Notice: Undefined index: cNetworkID in E:\Inet\saveadd_callaway.php on line 12
Notice: Undefined index: cColorChoice in E:\Inet\saveadd_callaway.php on line 13
Notice: Undefined index: SubmittedOn in E:\Inet\saveadd_callaway.php on line 14
 - - - - 

Even if I use isset() all that does is not show me the Notice details (Undefined index...) I still get an error. Why does it work sometimes, but not others? Even when I get an error - I can click refresh and then it works! How is that?

  • if it is possible to get to the second page without any data being posted then you will get the warnings you mention.. I suggest checking for `if(count($_POST) > 0)` or something along those lines to make sure you have post data to display – Dale Apr 22 '16 at 13:54
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – urfusion Apr 22 '16 at 13:57
  • Like I said, I'm still learning.... but could closing the db link matter? I connect to mysqli on both pages. but I do not close the link on either. I have tried the 4 combinations of closing thing the link on the two pages (both with, both without, one with & one without, and vice-versa) but I still have the same intermittent success. I learned thru another post that the db link closes at the end of the page... so I'm not sure it matters... just figured I'd offer this info too! :) Thanks! – Shleeenanigans Apr 22 '16 at 13:58
  • DALE: It just gives me the same errors. – Shleeenanigans Apr 22 '16 at 14:05
  • URFUSION: Can you help me understand what you're suggesting? Should I change the variable names so that they are different than the $_POST data names? (on saveaddcallaway.php)? – Shleeenanigans Apr 22 '16 at 14:06
  • ... I just did that & still no go. But thanks for the quick attention and suggestions! I'm beating my head against the wall. – Shleeenanigans Apr 22 '16 at 14:11
  • did you notice that form action is 'saveadd_callaway.php' and php file name is 'saveaddcallaway.php' ? – Ravinder Reddy Apr 22 '16 at 14:13
  • REDDY: That's my typo in this post...sorry. But good catch... those kinds of things hold me up for days... ha! The file is saveadd_callaway.php & is referenced correctly in the form action. – Shleeenanigans Apr 22 '16 at 14:26
  • error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR ); disable notice error – user5200704 Apr 22 '16 at 14:29

1 Answers1

0

Try this

<!--saveadd_callaway.php-->
<?php 
// checks if the request is by post method and $_POST array have some values
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (count($_POST)>0)){
     // get all post data
     // insert into database
}else{
    header("Location: callaway.php");
    exit;
}
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
  • I tried - but this just sends me back to the form page (refreshed) after I hit submit... (when it errors which is like 1 out of 4 tries). It doesn't fix/explain what the problem is and why it didn't process. – Shleeenanigans Apr 22 '16 at 14:30