jahicki Posted September 27, 2020 Share Posted September 27, 2020 (edited) Hi guys, I have this code: <?php // This Code Works if (isset($_POST{"PantryID"})) { Create_Pantry($_POST{"PantryID"}); } // This Code Isn't Working, Keeps Going To 'Else'. if (isset($_POST{"ItemName"})){ Add_Item($_POST{"ItemName"}, $_POST{"ItemWeight"}, $_POST{"ItemPrice"}, $_POST{"ItemDate"}); }else{ echo "Nothing To do"; } ?> here is the html <label for="PantryID">Create Pantry: </label> <input type="text" id="PantryID" name="PantryID" disabled="true" /> <label for="ItemName">Item Name: </label> <input type="text" id="ItemName" name="ItemName" disabled="true" /> Please help, I can't find any syntax errors. - At the point the PHP is activated it is no longer disabled. the PantryID is set up the same way. Edited September 27, 2020 by jahicki Correction Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/ Share on other sites More sharing options...
gizmola Posted September 27, 2020 Share Posted September 27, 2020 Add a debugging statement at the top of your script of var_dump($_POST) and verify your assumptions. Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581606 Share on other sites More sharing options...
jahicki Posted September 27, 2020 Author Share Posted September 27, 2020 I am new to PHP and Debugging I got this from the Var_Dump: array(1) { ["Submit"]=> string(6) "Submit" } Array Any Help Would Be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581607 Share on other sites More sharing options...
requinix Posted September 27, 2020 Share Posted September 27, 2020 44 minutes ago, jahicki said: At the point the PHP is activated it is no longer disabled. How is that accomplished? You wouldn't happen to be setting disabled to "false", would you? Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581608 Share on other sites More sharing options...
Phi11W Posted September 28, 2020 Share Posted September 28, 2020 16 hours ago, jahicki said: You: ... if (isset($_POST{"ItemName"})) ... Gizmola: ... var_dump($_POST) ... You: ... I got this from the Var_Dump: array(1) { ["Submit"]=> string(6) "Submit" } Array Conclusion? The ItemName Form field is not being passed across. Check the HTML that should be sending it. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581615 Share on other sites More sharing options...
benanamen Posted September 28, 2020 Share Posted September 28, 2020 (edited) You can start by using square brackets in your POST variables instead of curly braces like the rest of us. The curly braces can fail in at least one case. Example: $myArray = [1,2]; $index = 1; echo "value at index $index is $myArray[$index]"; // outputs "value at index 1 is 2" echo "value at index $index is $myArray{$index}"; // will throw "Notice: Array to string conversion" var_dump($myArray{$index}); // outputs "int(2)" https://wiki.php.net/rfc/deprecate_curly_braces_array_access Edited September 28, 2020 by benanamen 1 Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581619 Share on other sites More sharing options...
gizmola Posted September 28, 2020 Share Posted September 28, 2020 So you have 2 items you can work on. The first was brought up by benanamen, which is really a syntax issue. You are not referencing arrays correctly: // Use this if (isset($_POST['itemName']) { // Not this if (isset($_POST{'itemName'}) { That is not the source of your current problem however. Your current problem is that whatever javascript you have that you assume is making your form valid, is not valid, so your text field is not being passed when the form is posted. Another good debugging tool is to use something like chrome dev tools, and look at the network tab when you submit the form. You can then see the contents of the post variables as they are sent when you submit the form. In conclusion, right now your problem is your html/javascript code and has nothing to do with PHP. The code is working as you intended, as $_POST['itemName'] is not set/does not exist. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581620 Share on other sites More sharing options...
jahicki Posted September 29, 2020 Author Share Posted September 29, 2020 (edited) Hi guys, thanks for the responses. I removed the JS completely for now, as it isn't required. I changed the Curley Brackets for Square. (Can't remember where I saw the Curley Brackets.) Now I get this: array(4) { ["Item"]=> string(5) "Bacon" ["Weight"]=> string(3) "500" ["Price"]=> string(4) "3.25" ["Date"]=> string(10) "12/12/2020" } ArrayCollecting Data -- Establishing Connection To Database -- Building Query -- Bacon5003.2512/12/2020 -- Inserting Data -- Error creating table: Unknown column 'Bacon' in 'field list $sql = "INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate) VALUES (" . $Name . ", " . $Weight . ", " . $Price . ", " . $Date . ")"; Edited September 29, 2020 by jahicki Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581630 Share on other sites More sharing options...
Barand Posted September 29, 2020 Share Posted September 29, 2020 I hope you are not storing dates in your db table in that "12/12/2020" format. What is the structure of the table you are trying to update? Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581631 Share on other sites More sharing options...
jahicki Posted September 29, 2020 Author Share Posted September 29, 2020 (edited) 9 minutes ago, Barand said: I hope you are not storing dates in your db table in that "12/12/2020" format. What is the structure of the table you are trying to update? I won't be once I get it updating. its a basic SQL database table. the table was created with this code: $sql = "CREATE TABLE " . $Name . "( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ItemName VARCHAR(30) NOT NULL, ItemWeight VARCHAR(30) NOT NULL, ItemPrice VARCHAR(5), ItemDate DATE )"; Edited September 29, 2020 by jahicki Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581632 Share on other sites More sharing options...
jahicki Posted September 29, 2020 Author Share Posted September 29, 2020 Actually, I'll make a new topic in an SQL Forum... as I don't think this is a PHP issue any more. Thanks guys 1 Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581634 Share on other sites More sharing options...
Phi11W Posted September 29, 2020 Share Posted September 29, 2020 2 hours ago, jahicki said: $sql = "INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate) VALUES (" . $Name . ", " . $Weight . ", " . $Price . ", " . $Date . ")"; Never, never trust data values submitted by the User! You are wide open to SQL Injection Attacks. Obligatory XKCD Reference: Little Bobby Tables Read up on Prepared Statements or, at the very least, encode the values that you are using to build your "SQL" - a String variable that just happens to contains something that your DBMS can understand. And Barand is absolutely right - be extra careful with Date values. When is 03/06/09? Spring, Summer or Autumn? Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/311533-cant-detect-isset-for-text-box-only-1-box/#findComment-1581638 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.