Old_Dog_New_Tricks Posted June 14, 2023 Share Posted June 14, 2023 Recently I retired and decided to start learning a little about my computer and webpages. I'm doing ok with html and css so I thought I'd look at php. I seem to learn best by examples and then deconstructing those examples, so I asked the openai for a number randomiser based on the user providing a list of numbers, and it spat out some code, but it references an index.php file, and I don't know what to do. Can someone guide me to a solution? Here's my openai prompt: Create a basic webpage with a text box to accept input from the user, and a button to perform the action. Use HTML & PHP, don't use JS or CSS, to allow the user to input a list of numbers, strip out & remove any and all non-number characters, mix and randomise the numbers, then output the numbers in groups of 5 numbers, separated by a single space, sorted from lowest to highest, each group of 5 numbers is to be on its own line, each group separated by a blank line. Here's the provided code: <html> <head> <title>Number Sorter</title> </head> <body> <form action="index.php" method="post"> <input type="text" name="numbers" placeholder="Enter numbers here..."> <input type="submit" name="submit" value="Sort"> </form> <?php if(isset($_POST['submit'])) { $numbers = $_POST['numbers']; $numbers = preg_replace('/[^0-9]/', '', $numbers); $numbers = str_split($numbers); shuffle($numbers); sort($numbers); $numbers = array_chunk($numbers, 5); foreach($numbers as $number) { echo implode(' ', $number) . "<br><br>"; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/316973-not-sure-what-to-do-with-action-equals-indexphp/ Share on other sites More sharing options...
Solution Barand Posted June 14, 2023 Solution Share Posted June 14, 2023 It looks like it expects that code to be in "index.php". The posted data will be sent to that file (itself) If you remove action="index.php" you can call your php file anything and, without the action, it will call itself. Quote Link to comment https://forums.phpfreaks.com/topic/316973-not-sure-what-to-do-with-action-equals-indexphp/#findComment-1609686 Share on other sites More sharing options...
requinix Posted June 14, 2023 Share Posted June 14, 2023 55 minutes ago, Old_Dog_New_Tricks said: I seem to learn best by examples and then deconstructing those examples, That's good. 55 minutes ago, Old_Dog_New_Tricks said: so I asked the openai That's not good. For example, let me point out something: this shuffle($numbers); sort($numbers); randomly shuffles the $numbers and then sorts them in increasing order. It's nonsense, and not the sort of thing you should be trying to learn. "AI" is good at stringing words together in a way that, according to some calculated probabilities, hopefully make sense. But that is all it does - and it is certainly not intelligent. Be very careful with anything it says or creates. Quote Link to comment https://forums.phpfreaks.com/topic/316973-not-sure-what-to-do-with-action-equals-indexphp/#findComment-1609687 Share on other sites More sharing options...
Old_Dog_New_Tricks Posted June 14, 2023 Author Share Posted June 14, 2023 Thank you Barand, your suggestion worked and it printed the results to the same page. It took me a bit of searching but I found I had to install apache and then put the file in my var/www/html folder and call it using localhost/filename. The result from the script in the webpage is not what I expected, but that's another issue/topic and something to play with further before I give up, commence with the gnashing of teech, and beg for further assistance. Quote Link to comment https://forums.phpfreaks.com/topic/316973-not-sure-what-to-do-with-action-equals-indexphp/#findComment-1609688 Share on other sites More sharing options...
Old_Dog_New_Tricks Posted June 14, 2023 Author Share Posted June 14, 2023 3 minutes ago, requinix said: That's good. That's not good. For example, let me point out something: this shuffle($numbers); sort($numbers); randomly shuffles the $numbers and then sorts them in increasing order. It's nonsense, and not the sort of thing you should be trying to learn. "AI" is good at stringing words together in a way that, according to some calculated probabilities, hopefully make sense. But that is all it does - and it is certainly not intelligent. Be very careful with anything it says or creates. I agree, the results from the script are not helpful and not what I expected. I used JS first, and it did what I wanted, but the number generation is far from truly being random - there's obvious patterns to the number order generated, so that's why I was looking at php. This is a first experiment, so I have a bit to learn, obviously, both about php and about even how to correctly prompt the ai to obtain the results for which I'm searching. I appreciate your words of caution, but it's worth noting I'm not trying to solve any world problems so anything I get from the ai is not going to be the cause of a catastrophe. I'm simply playing around and experimenting to obtain some code to take apart. But, again, I appreciate your cautionary warning. Quote Link to comment https://forums.phpfreaks.com/topic/316973-not-sure-what-to-do-with-action-equals-indexphp/#findComment-1609689 Share on other sites More sharing options...
dodgeitorelse3 Posted June 15, 2023 Share Posted June 15, 2023 (edited) Using sort will sort numbers. so I if enter 12345 into input box results will be 12345. If I do same thing again then result will be the same. Without using sort, the numbers will be returned in different orders each time using same input of 12345. However entering more than 5 digits, lets say 1 through 9, then it will return 2 sets of numbers. the first set with 5 numbers and the second set with the remaining 4 numbers. So depending on what you are expecting for results depends on the code you use. Edited June 15, 2023 by dodgeitorelse3 typo Quote Link to comment https://forums.phpfreaks.com/topic/316973-not-sure-what-to-do-with-action-equals-indexphp/#findComment-1609693 Share on other sites More sharing options...
gizmola Posted June 19, 2023 Share Posted June 19, 2023 Far better ways of learning PHP: https://phptherightway.com/ Learning PDO (Relational Database API) https://phpdelusions.net/pdo If you are a visual learner, who likes streaming video courseware: Quote Link to comment https://forums.phpfreaks.com/topic/316973-not-sure-what-to-do-with-action-equals-indexphp/#findComment-1609817 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.