phppup Posted November 26, 2019 Share Posted November 26, 2019 I have an index HTML form that has a JavaScript link and action = 'xyz.php' Everything works fine. I've seen PHP scripts with HTML form after the closing tag ?> and they work. Yet when I included my HTML g form after my PHP code, some of my JS stopped working. What are the protocols to combining PHP and HTML? Which should run first? When is the action run? Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/ Share on other sites More sharing options...
maxxd Posted November 26, 2019 Share Posted November 26, 2019 (edited) You're going to have to post your code. PHP inside HTML may not be the best idea in the world, but it works pretty flawlessly assuming everything is done correctly. My bet is that if your JavaScript stopped working, something didn't get named what it is later called and the error is pretty standard once you see it. As far as which should be done first (PHP/HTML/JavaScript), if you're determined to combine PHP and HTML, put the PHP code first. Deal with any form handling, collate the page variables, and deal with the system business logic, then render the HTML display. Keep your JavaScript entirely separate from all of that so that you can minimize and uglify it via webpack or gulp. This will at least get you a step or two closer to true separation of concerns, and that'll save you refactoring time in the future. Oh - and a form action is "run" when the form is submitted. Nothing's actually run as such, the browser redirects to the page named in the form's action property and passes the data from the form to the page via either $_GET or $_POST, depending on the method attribute of the forms. Edited November 26, 2019 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1571899 Share on other sites More sharing options...
Psycho Posted November 26, 2019 Share Posted November 26, 2019 To add to @maxxd's response. When a user requests a PHP page from a web server, the web server will first execute any php code in the page. The results after the php code is execute is then sent to the user/browser. If you are having problems in the page rendered in the browser, there is something off in the output (HTML, Javascript, etc.) sent to the browser. It can get confusing when you are using a programming language (PHP) to dynamically create other code (JavaScript) or Markup (HTML). Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1571910 Share on other sites More sharing options...
phppup Posted November 26, 2019 Author Share Posted November 26, 2019 I already have a working PHP/HTML uploading form that I've been working on and tweaking as a single PHP file. Now, I am attempting to integrate a progress bar. It seems almost impossible to find a working script to show the progress of multiple files as they upload. However, I have one that is "good enough" and have broken it down thusfar so that I can effectively integrate it into my working script. At this point, (aide from the JQuery, CSS, etc.) it consists primarily of two files: The first is a PHP file containing the form and basic links for function and style. The second is a PHP upload file that is handling the "heavy lifting." I have broken it down to this: $dir = 'uploads/'; $count = 0; if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files'])) { // loop all files foreach ( $_FILES['files']['name'] as $i => $name ) { // now we can move uploaded files if( move_uploaded_file($_FILES["files"]["tmp_name"][$i], $dir . $name) ) $count++; } echo json_encode(array('count' => $count)); } No names or file locations have been changed and it functions as originally designed in this condition. However, if I combine the first file with this file, I loose the echo json_encode(array('count' => $count)); result, which is expanded in another file to be a final message of "You have uploaded $count files successfully." What am I doing wrong or missing? Is there an easier way to do this? A link to a fancy progress bar for multiple file upload would be wonderful, as most that I've found actually do not work. Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1571913 Share on other sites More sharing options...
maxxd Posted November 27, 2019 Share Posted November 27, 2019 When I asked you to post the code, I meant for you to post the code that's actually being run. You mention a 'first' script that you don't show us, and then talk about what doesn't happen when you "combine" that script with what you do show. How are you combining the scripts? What does the resulting script look like? Right now you're asking us to tell you why your shower isn't working by telling us you have a bathroom. Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1571925 Share on other sites More sharing options...
phppup Posted November 27, 2019 Author Share Posted November 27, 2019 I don't have access to my computer at the moment, but... The first script is a basic HTML form (with some JS and CSS links) that is saved as a PHP file. To combine them, I have simply cut and pasted the form code (in its entirety) below the PHP code (displayed previously). I have not changed any file names or paths. Also, after deeper investigation, I saw a line in a jQuery file (not my forte) that was written as "data.html" . Is it possible that it is falling to find "data" because AFTER my modification there is no longer an HTML file? If this is true, can I redirect it to the PHP file? Perhaps a better question would be "What is the benefit to keeping my form and the PHP that process it within a single file? Thanks for the help and HAPPY THANKSGIVING. Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1571930 Share on other sites More sharing options...
phppup Posted December 3, 2019 Author Share Posted December 3, 2019 Is there a benefit to keeping my form and the PHP that processes it within a single file? Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1572156 Share on other sites More sharing options...
mac_gyver Posted December 3, 2019 Share Posted December 3, 2019 (edited) 14 minutes ago, phppup said: Is there a benefit to keeping my form and the PHP that processes it within a single file? Yes. It provided a better user experience, since you can directly display any validation errors when you re-display the form and you can re-populate the form fields with the previously submitted data so that the user doesn't need to keep re-entering the same values over and over. It also takes less code over doing these things on two separate pages since you don't need to pass error information and field values between pages. The only things that are needed to accomplish this are 1) put the form processing code above the start of the html document and 2) put the form processing code inside of a conditional statement that detects if a post method form has been submitted so that it only runs if the form has been submitted. Edited December 3, 2019 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1572157 Share on other sites More sharing options...
Psycho Posted December 5, 2019 Share Posted December 5, 2019 On 12/3/2019 at 4:46 PM, mac_gyver said: Yes. It provided a better user experience, since you can directly display any validation errors when you re-display the form and you can re-populate the form fields with the previously submitted data so that the user doesn't need to keep re-entering the same values over and over. It also takes less code over doing these things on two separate pages since you don't need to pass error information and field values between pages. None of what you described requires the code to be in the same page or not. For small projects, it's fine. But, it is significantly easier to manage code when you have separate files based on separate purposes. Quote Link to comment https://forums.phpfreaks.com/topic/309581-php-and-html-in-same-script/#findComment-1572229 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.