new2you Posted October 30, 2014 Share Posted October 30, 2014 Hello All, I'm kinda new to PHP and web development and I seem to have a very wierd problem which is driving me nuts (for the last 2 days)...so I'd appreciate any and all help in getting it resolved. Okay, so here's the problem: In the code below, I used to have just the first statement below...which essentially accepted input as a textbox for a text field named qty_pkg (in the underlying MySQL table)...and that used to work just fine. So then I decided to change the input type for that field to be a radio button, accepting one of two choices i.e. "UoM" or "Qty", and therefore I added the remainder of the code, whilst also commenting/remarking out the first statement. What happens (when I added the radiobutton-related code) is that control no longer goes into the TRUE portion/logic of the "if ( !empty($_POST))" condition-check, but rather, it seems to go into the "ELSE/FALSE" portion of that conditional check. However, a record does get added to my table, even though I do not have an "INSERT INTO..." clause in the ELSE porition of that check. Also, what gets displayed on screen is as follows: ------------------------------------------------------------------------------------------------------------------------------------ Array() There's something wrong somewhere!!! Array() ------------------------------------------------------------------------------------------------------------------------------------ Now, if/when I comment/remark the radiobutton-related code, the "if ( !empty($_POST))" logic seems to work fine and the following is displayed on screen (which is what I expect): ------------------------------------------------------------------------------------------------------------------------------------ Array ( [datepicker] => 10/30/2014 [store_id] => 48 [new_store_name] => [item_id] => 5 [new_item_name] => [pkg_of] => 1 [price] => 1 [flyer_page] => 1 [limited_time_sale] => Array ( [0] => fri [1] => sat ) [nos_to_purchase] => 1 [create-and-add-more] => Create and Add More )Notice: Undefined index: qty_pkg in /var/www/create.php on line 54Array ( [0] => Array ( [item_id] => 5 [0] => 5 [item_name] => BD Cheese Strings [1] => BD Cheese Strings [section_id] => 7 [2] => 7 ) ) ------------------------------------------------------------------------------------------------------------------------------------ The other wierd thing is that if/when I now uncomment the first statement (which used to work just fine before) the "if ( !empty($_POST))" logic no longer seems to work the way it used to. Does/can someone see something wrong with my code somewhere? <!-- <?php echo standardInputField('Qty / Pkg', 'qty_pkg', $qty_pkg, $errors); ?> --> <div class="control-group"> <label class="control-label">Priced by:</label> <div class="priced-by-radio-container"> <div class="pb-radiobuttons-container"> <label class="indent-to-the-left"> <input class='priced-by-radiobutton' type='radio' name='qty_pkg' value='UoM' $pb1> <span class='no-highlight'>Unit of Measure</span> </label> <label> <input class='priced-by-radiobutton' type='radio' name='qty_pkg' value='Qty' $pb2> <span class='no-highlight'>Quantity</span> </label> </div> </div> </div> <?php echo standardInputField('UoM Name/Pkg. Of:', 'pkg_of', $pkg_of, $errors); ?> <?php echo standardInputField('Price:', 'price', $price, $errors); ?> <?php echo standardInputField('Flyer Page #:', 'flyer_page', $flyer_page, $errors); ?> print_r($_POST); if ( !empty($_POST)) { // keep track of $_POST(ed) values by storing the entered values to memory variables $store_id = $_POST['store_id']; $item_id = $_POST['item_id']; $qty_pkg = $_POST['qty_pkg']; $pkg_of = $_POST['pkg_of']; ... ... $sql = "INSERT INTO shoplist (store_id,item_id,qty_pkg,pkg_of,price,flyer_page,limited_time_sale,flyer_date_start,nos_to_purchase,section_id,shopper1_buy_flag,shopper2_buy_flag,purchased_flag,purchase_later_flag,no_flag_set) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($store_id,$item_id,$qty_pkg,$pkg_of,$price,$flyer_page,$limited_time_sale,$flyerDateStart,$nos_to_purchase,$section_id,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_Y)); } else { echo "<br />There's something wrong somewhere!!!<br />"; print_r ($_POST); $qty_pkg = ''; $pkg_of = '';; Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/ Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 Sorry - can't follow what you are saying. Too many interruptions between your dialog and your code. Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495259 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2014 Share Posted October 30, 2014 Have you validated your HTML? http://validator.w3.org/ Do you have a form around your inputs? What is $pb1 and $pb2 all about? Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495262 Share on other sites More sharing options...
new2you Posted October 30, 2014 Author Share Posted October 30, 2014 @NotionCommotion, I tried validating my code but wasn't successful since I'm currently developing this on a non-public IP address..and the validator no longer works for non-public IP addresses. @ginerjm, My apologies for making it look so confusing...hope it's easier to understand now :-) In the code below, I used to have just the first statement...which essentially accepted input as a textbox for a text field named qty_pkg (in the underlying MySQL table)...and that used to work just fine. <!-- <?php echo standardInputField('Qty / Pkg', 'qty_pkg', $qty_pkg, $errors); ?> --> <div class="control-group"> <label class="control-label">Priced by:</label> <div class="priced-by-radio-container"> <div class="pb-radiobuttons-container"> <label class="indent-to-the-left"> <input class='priced-by-radiobutton' type='radio' name='qty_pkg' value='UoM' $pb1> <span class='no-highlight'>Unit of Measure</span> </label> <label> <input class='priced-by-radiobutton' type='radio' name='qty_pkg' value='Qty' $pb2> <span class='no-highlight'>Quantity</span> </label> </div> </div> </div> <?php echo standardInputField('UoM Name/Pkg. Of:', 'pkg_of', $pkg_of, $errors); ?> <?php echo standardInputField('Price:', 'price', $price, $errors); ?> <?php echo standardInputField('Flyer Page #:', 'flyer_page', $flyer_page, $errors); ?> So then I decided to change the input type for that field to be a radio button, accepting one of two choices i.e. "UoM" or "Qty", and therefore I added the remainder of the code, whilst also commenting/remarking out the first statement. Now, what happens (when I added the radiobutton-related code above) is that control no longer goes into the TRUE portion/logic of the "if ( !empty($_POST))" condition-check - shown in the code below - but rather, it seems to go into the "ELSE/FALSE" portion of that conditional check. However, a record does get added to my table, even though I do not have an "INSERT INTO..." clause in the ELSE porition of that check. print_r($_POST); if ( !empty($_POST)) { // keep track of $_POST(ed) values by storing the entered values to memory variables $store_id = $_POST['store_id']; $item_id = $_POST['item_id']; $qty_pkg = $_POST['qty_pkg']; $pkg_of = $_POST['pkg_of']; ... ... $sql = "INSERT INTO shoplist (store_id,item_id,qty_pkg,pkg_of,price,flyer_page,limited_time_sale,flyer_date_start,nos_to_purchase,section_id,shopper1_buy_flag,shopper2_buy_flag,purchased_flag,purchase_later_flag,no_flag_set) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($store_id,$item_id,$qty_pkg,$pkg_of,$price,$flyer_page,$limited_time_sale,$flyerDateStart,$nos_to_purchase,$section_id,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_Y)); } else { echo "<br />There's something wrong somewhere!!!<br />"; print_r ($_POST); $qty_pkg = ''; $pkg_of = '';; Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495270 Share on other sites More sharing options...
Psycho Posted October 30, 2014 Share Posted October 30, 2014 OK, I agree with ginerjm. You are providing way too much information. This makes it difficult for us to understand what you are asking and then expecting us to read through a bunch of code to try and decipher decreases your chances of getting a response. You need to learn how to reduce the code (and explanation) to what is pertinent. What I understand is that the condition to check if the super global $_POST is empty is not generating the results you think it should. Specifically, you are getting this on the page: Array() There's something wrong somewhere!!! Array() Then, looking at just the relevant code, you have this print_r($_POST); //##### if ( !empty($_POST)) { // keep track of $_POST(ed) values by storing the entered values to memory variables $store_id = $_POST['store_id']; $item_id = $_POST['item_id']; $qty_pkg = $_POST['qty_pkg']; $pkg_of = $_POST['pkg_of']; // . . . // . . . $sql = "INSERT INTO shoplist (store_id,item_id,qty_pkg,pkg_of,price,flyer_page,limited_time_sale,flyer_date_start,nos_to_purchase,section_id,shopper1_buy_flag,shopper2_buy_flag,purchased_flag,purchase_later_flag,no_flag_set) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($store_id,$item_id,$qty_pkg,$pkg_of,$price,$flyer_page,$limited_time_sale,$flyerDateStart,$nos_to_purchase,$section_id,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_Y)); } else { echo "<br />There's something wrong somewhere!!!<br />"; //##### print_r ($_POST); //##### // . . . The ones I commented with "#####" are the ones producing the output. So, that tells me that $_POST is empty. But, you say that a record is getting created based on the code in the TRUE part of the conditional block. That is impossible based on what you posted. Either this isn't the right code, there is other code that isn't being displayed, or you are misinterpreting the results. To verify, just add some additional debugging logic echo "Count of $_POST var: " . count($_POST) . "<br>\n"; if ( !empty($_POST)) { echo "Starting the code block for the TRUE condition<br>"; // keep track of $_POST(ed) values by storing the entered values to memory variables $store_id = $_POST['store_id']; $item_id = $_POST['item_id']; $qty_pkg = $_POST['qty_pkg']; $pkg_of = $_POST['pkg_of']; // . . . // . . . $sql = "INSERT INTO shoplist (store_id,item_id,qty_pkg,pkg_of,price,flyer_page,limited_time_sale,flyer_date_start,nos_to_purchase,section_id,shopper1_buy_flag,shopper2_buy_flag,purchased_flag,purchase_later_flag,no_flag_set) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($store_id,$item_id,$qty_pkg,$pkg_of,$price,$flyer_page,$limited_time_sale,$flyerDateStart,$nos_to_purchase,$section_id,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_Y)); echo "Executed SQL query: $sql<br>"; } else { echo "Starting the code block for the FALSE condition<br>"; echo "<br />There's something wrong somewhere!!!<br />"; print_r ($_POST); // . . . Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495275 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2014 Share Posted October 30, 2014 @NotionCommotion, I tried validating my code but wasn't successful since I'm currently developing this on a non-public IP address..and the validator no longer works for non-public IP addresses. $_POST is empty, so you are not correctly posting data from the client to the server. As such, it has nothing to do with your script that handles the POST data, and only your script which creates the initial HTML. To troubleshoot, the first thing you should do is verify that the rendered HTML is valid. Often, a site requires authentication and sessions, and the validation site cannot handle. As such, the best approach is direct input of HTML using http://validator.w3.org/#validate_by_input. To obtain the HTML, don't use the PHP script directly, but use your browser to display the HTML. Only once you know you are posting the correct data, start working on the script that validates/saves the data. Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495284 Share on other sites More sharing options...
new2you Posted October 30, 2014 Author Share Posted October 30, 2014 Guys, thanks a lot for taking the time to read, understand, and reply to my plea for help (especially you @Psycho, since you understood my very first post perfectly)...much appreciated! Okay, so here's what I did (after trying almost all of the suggestions provided by you wonderful folks), and what I found out: I reverted to an older copy of my create.php file (wherein I was accepting all input in the form of textboxes only...and not as radiobuttons, checkboxes etc.) and found that the problem existed (all along) and in that code as well, and that the problem seems to be related to the following line, which if/when I comment out will allow control to go into the TRUE portion. But, as long as the following line is not commented out, control does not go into the TRUE condition, but yet, the record does get created - for some strange reason. <?php echo standardInputField('Qty/Pkg', 'qty_pkg', $qty_pkg, $errors); ?> Other than saying that I'm stumped, I have no idea what else I can do, or even say...because this just does not make sense to me. I certainly don't wish to give up, but at the same time I don't know how to move on, and get this resolved....catch 22! Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495296 Share on other sites More sharing options...
new2you Posted October 30, 2014 Author Share Posted October 30, 2014 I'm now wondering/hoping someone can check the following function to see if there might be something wrong in there. I ask this because I've now found out that it's not specifically the statement in the post above that is causing the problem, but any similar statement (for a different field/value) is also causing similar behaviour...so perhaps it might be something to do with the function. //Since you want the format for the input fields to be the same, //You can create a function to create them function standardInputField($fieldLabel, $fieldName, $fieldValue, $errorsAry) { $fieldHTML = "<div class='control-group'>\n"; $fieldName == "new_store_name" || $fieldName == "new_item_name" ? $fieldHTML .= " <label id='label-new-store-or-item' class='control-label'>{$fieldLabel}</label>\n" : $fieldHTML .= " <label class='control-label'>{$fieldLabel}</label>\n"; $fieldHTML .= " <div class='controls'>\n"; $fieldName == "new_store_name" || $fieldName == "new_item_name" ? $fieldName == "new_store_name" ? $fieldHTML .= " <input class='other-store-or-item' id='new_store_name' type='text' name='{$fieldName}' value='{$fieldValue}' />\n" : $fieldHTML .= " <input class='other-store-or-item' id='new_item_name' type='text' name='{$fieldName}' value='{$fieldValue}' />\n" : $fieldHTML .= " <input type='text' name='{$fieldName}' value='{$fieldValue}' />\n"; //If the field has an error, display it. if(isset($errorsAry) && in_array($fieldName, $errorsAry)) { $fields = fieldsArrayForCreateUpdate(); $fieldHTML .= " <span class='error-message'>" . $fields[$fieldName]['error'] . "</span>\n"; } $fieldHTML .= " </div>\n"; $fieldHTML .= "</div>\n"; return $fieldHTML; } Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495302 Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 I've never seen so many labels, divs, and classes in such a small html form. Whatever are you doing? You have a lable within a label - what is that supposed to be? You have 3 divs at one point for 2 fields. Why? Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495303 Share on other sites More sharing options...
new2you Posted October 30, 2014 Author Share Posted October 30, 2014 I've gone so blind to this code that I now cannot even see the "label within label" that you're refering to. Where do you see it? Essentially this function is being used in order to avoid having to hard-code the same HTML for multiple fields (which were of the same input type). While it did serve to shorten the code (albeit slightly), it sure did add a fair amount of complexity to the code, I'm sure. But now that I think of it, perhaps I should actually hard-code separately for each field, especially now that I'm changing some textboxes to radiobutton and/or checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495306 Share on other sites More sharing options...
Psycho Posted October 30, 2014 Share Posted October 30, 2014 @new2you, there is nothing in that code that would wipe out the $_POST array, but there is another function call in that function $fields = fieldsArrayForCreateUpdate(); @ginerjm, the code does not have labels within labels. There is only one label. His code is overly complex and utilizes the ternary operator where, in my opinion, makes it more difficult to read. Here is a rewrite that woud do the same thing that is more logical //Since you want the format for the input fields to be the same, //You can create a function to create them function standardInputField($fieldLabel, $fieldName, $fieldValue, $errorsAry) { //Determine label ID and input ID $fieldId = ($fieldName == "new_store_name" || $fieldName == "new_item_name") { $labelID = 'label-new-store-or-item'; $inputID = 'new_store_name'; } else { $labelID = ''; $inputID = 'new_item_name'; } //Determine error message if(isset($errorsAry) && in_array($fieldName, $errorsAry)) { $fields = fieldsArrayForCreateUpdate(); $error .= " <span class='error-message'>{$fields[$fieldName]['error']}</span>\n"; } else { $error = "": } $fieldHTML = "<div class='control-group'>\n"; $fieldHTML .= " <label id='{$labelID}' class='control-label'>{$fieldLabel}</label>\n"; $fieldHTML .= " <div class='controls'>\n"; $fieldHTML .= " <input class='other-store-or-item' id='{$inputID}' type='text' name='{$fieldName}' value='{$fieldValue}' />\n" $fieldHTML .= $error; $fieldHTML .= " </div>\n"; $fieldHTML .= "</div>\n"; return $fieldHTML; } Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495310 Share on other sites More sharing options...
kicken Posted October 30, 2014 Share Posted October 30, 2014 I tried validating my code but wasn't successful since I'm currently developing this on a non-public IP address..and the validator no longer works for non-public IP addresses. Use the Direct Input tab on the validator. Load your page in your browser and View-Source. Copy and paste that into the Direct Input tab on the validator and hit validate. But, as long as the following line is not commented out, control does not go into the TRUE condition, but yet, the record does get created - for some strange reason. It is simply not possible for the code to hit the else block and create the record within the same request with the code shown. Most likely explanations that might cause the behavior are: Your code is being run twice, either in separate requests or in the same request. The code you are showing us does not accurately represent the code you are running. You are incorrectly diagnosing the problem Make sure you are actually seeing the symptoms correctly. For example replace one of your column's value in the INSERT with a fixed value and then see if that value is shown in the table after running the script. Make sure you are not running the script twice. Check your browser's debugging tools for the list of requests it is making. Check your server's access logs for the requests it's receiving. Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495324 Share on other sites More sharing options...
new2you Posted October 31, 2014 Author Share Posted October 31, 2014 @Psycho, much thanks for re-writing and simplyfying that function of mine. It looks waaaay simpler now - very easy to understand and debug. So, here's another thing I found out (last night), when I decided to temporarily stop using the function and instead duplicated the HTML for all fields. When I have all the input fields (6 of them) being processed (i.e. uncommented), the wierd behaviour rears its ugly head i.e. I don't see the echo statement that says that reads ""Starting the code block for the TRUE condition", but yet a record gets created. @kicken: as you suggested, I did change my "INSERT INTO..." statement to hard-code a value for one of the fields, and yes, the hard-coded value is being inserted into the table...which means that the contol is somehow getting into the TRUE portion, but without displaying a number of echo statements that I've put in place. Spooky 'eh? Perhaps I should wait until after Halloween, and see if the ghosts have left my code :-) Just kidding! Anyways, the other thing is that if/when I remark out any one of those fields (and I mean, any one), the code seems to work the way I expect it to work. So, why is it that when input for 5 fields are being requested it works fine, but yet, when a 6th field is being added it goes crazy? Any further ideas/suggestions for me to try? This one is really driving me insane! The changed code is below: <!-- Commenting out this block of code, and replacing it with the block of code just below this one <?php echo standardInputField('Qty/Pkg', 'qty_pkg', $qty_pkg, $errors); ?> <?php echo standardInputField('Pkg. Of', 'pkg_of', $pkg_of, $errors); ?> <?php echo standardInputField('Price', 'price', $price, $errors); ?> <?php echo standardInputField('Flyer Page #', 'flyer_page', $flyer_page, $errors); ?> <?php echo standardInputField('Limited Time Sale', 'limited_time_sale', $limited_time_sale, $errors); ?> <?php echo standardInputField('No(s) to Purchase', 'nos_to_purchase', $nos_to_purchase, $errors); ?> --> <!-- The code below is now being used instead of the block above --> <div class="control-group"> <label class='control-label'>Qty/Pkg:</label> <div class='controls'> <input type="text" name="qty_pkg" id="qty_pkg"> </div> </div> <div class="control-group"> <label class='control-label'>Pkg. Of:</label> <div class='controls'> <input type="text" name="pkg_of" id="pkg_of"> </div> </div> <div class="control-group"> <label class='control-label'>Price:</label> <div class='controls'> <input type="text" name="price" id="price"> </div> </div> <div class="control-group"> <label class='control-label'>Flyer Page #:</label> <div class='controls'> <input type="text" name="flyer_page" id="flyer_page"> </div> </div> <div class="control-group"> <label class='control-label'>Limited Time Sale:</label> <div class='controls'> <input type="text" name="limited_time_sale" id="limited_time_sale"> </div> </div> <div class="control-group"> <label class='control-label'>Nos to Purchase:</label> <div class='controls'> <input type="text" name="nos_to_purchase" id="nos_to_purchase"> </div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495379 Share on other sites More sharing options...
Psycho Posted October 31, 2014 Share Posted October 31, 2014 I don't see the echo statement that says that reads ""Starting the code block for the TRUE condition", but yet a record gets created. Then as we've already described, there must be other code that is doing the insert or the code is being run twice. For example, it could be running once to insert the record, but then does a redirect back to itself and runs the false condition. There is definitely something going on here that is not in the code you have provided. What is in that other function I identified which is being called? fieldsArrayForCreateUpdate() Quote Link to comment https://forums.phpfreaks.com/topic/292166-help-me-resolve-a-wierd-problem-in-a-form-with-radio-buttons/#findComment-1495385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.