-1

I'm having a hard time grasping how PHP/HTML/the server works and I couldn't find any info (it has to be somewhere so if you have a link I'd appreciate it). I'll try to explain best what I understand/don't understand and ask a concise question.

My Understanding

  1. PHP code is in the same file as HTML files and needs the server to have a PHP parser? to read and execute the PHP code.
  2. PHP code executes first on the server side, then HTML/JS execute on the client side (hence you can't see the PHP code on the browser).

What I don't understand

  1. When a PHP code calls a $_POST request, where is the request going? Let's say I call a $_POST request to mypage2.php. How do I handle this $_POST request in my mypage2.php code? (I setup an iFrame and tried to make a $_POST request to it, but it changed to that page).
  2. How do I use multiple .php files on a site? I always see something like authentication.php but are those pages just classes to never be used (aka I can never go to mysite.com/authentication.php)?
  3. My end goal is to get data from an external $_POST call, and send it to my page embedded in an iFrame. I feel I don't understand the basic concepts first though and all the tutorials discuss the syntax of PHP (which I think I understand).

I am running on a windows machine and using XAMPP if that is relevant. Thank you.

i_is_it
  • 15
  • 2
  • post your code what you have tried. – Saurabh Bhandari Feb 01 '17 at 08:23
  • 1
    I suggest you study more. – Mark Vincent Manjac Feb 01 '17 at 08:28
  • There are some mistakes in what you say : first, HTML is not executed, it is just a structuration language : it tells the browser how to render the web page. Second, `$_POST` is a variable in PHP and POST is a method of HTTP requests (two different things). Finally, there are many ways to use multiple php files : like in html, one php file per page (but this is a bad practice as you will have a less maintainable code and some code duplication) the other way is to include files from others. – ᴄʀᴏᴢᴇᴛ Feb 01 '17 at 08:29
  • 1
    you really need to study more because almost all answers are in php tutorials. Moreover, I don't think this question is on topic here because you don't have a specific programming problem. – ᴄʀᴏᴢᴇᴛ Feb 01 '17 at 08:31
  • Thanks for the comments, but if you look at the PHP tutorials, w3schools, tutorialspoint, codecademy, they teach you the syntax and setup but not how the language actually interacts (what I don't understand). I went through the w3schools and codecademy tutorial. – i_is_it Feb 01 '17 at 10:00

2 Answers2

1

Your html POST going to $_POST array on page2.php.

This array has the same index of page1.html input/form.

<input name="username"> <input name="password">

To use it in your page2.php, you can store it on a variable like this

$username = $_POST['username'];
$password = $_POST['password'];
Oscar Zarrus
  • 790
  • 1
  • 9
  • 17
0

You kind of have it right:

  1. PHP is in the same file with your HTML/CSS/JavaScript code. It requires a PHP interpreter on the Web server. The Web server will not send a raw PHP file (with PHP code in it) to a client browser, if configured correctly.

  2. When there is a request for a PHP file, the PHP interpreter on the Web server processes the PHP file, which removes the PHP code and replaces it with the interpreted result (HTML/CSS/JavaScript code). It sends the processed file to the client browser as the PHP file. That file only contains HTML/CSS/JavaScript code. The client browser runs the HTML/CSS/JavaScript code that the Web server sends.

  3. There are global variables in PHP that store data that's POSTed or otherwise sent to the PHP file. Have a look at $_POST, $_GET and $_REQUEST. Note that you should never trust data that's submitted by a user and you should always "sanitize" it (see What's the best method for sanitizing user input with PHP?)

  4. You can use more than one PHP file on a website, but no variables or other data carry automatically from file to file, so each file starts with a blank sheet. There are ways to pass values among pages by passing them in the URL or by using PHP Sessions.

  5. Some applications that use more than one PHP file don't want a user to be able to run a PHP file on its own. They only want the PHP file to be executed by the application, from another PHP file. So what the programmer will sometimes do is to set a global variable and store it as a session variable. The first thing a PHP file does is check that variable to see if it's set. If it's not, it means the user tried to run the PHP file by itself, and it just exits. That might be why you can't run a particular PHP file you mentioned.

  6. If you have a file mypage2.php and you post 'mydata' to that file, for example from an HTML form where name='mydata', you can retrieve the data from the global $_POST, for example, <?php $mydata = $_POST['mydata'];?>. After you sanitize it, if the IFRAME is also within mypage2.php, you can insert it into the IFRAME by echoing it, for example, <?php echo $mydata;?>

swmcdonnell
  • 1,381
  • 9
  • 22
  • Thanks for the very detailed response. Can I ask one question? When I made a POST request from my index.php through a form to my iFrame (mypage2.php), the mypage2.php page popped up in another tab. Can you explain how that happens? I thought only the request is being sent. – i_is_it Feb 01 '17 at 09:59