How to call php file in html

How to make a button call PHP?

I don't care if the page reloads or displays the results immediately;

Good!

Note: If you don't want to refresh the page see "Ok... but how do I Use Ajax anyway?" below.

I just want to have a button on my website make a PHP file run.

That can be done with a form with a single button:


  

That's it.

Pretty much. Also note that there are cases where ajax is really the way to go.

That depends on what you want. In general terms you only need ajax when you want to avoid realoading the page. Still you have said that you don't care about that.

Why I cannot call PHP directly from JavaScript?

If I can write the code inside HTML just fine, why can't I just reference the file for it in there or make a simple call for it in Javascript?

Because the PHP code is not in the HTML just fine. That's an illusion created by the way most server side scripting languages works [including PHP, JSP, and ASP]. That code only exists on the server, and it is no reachable form the client [the browser] without a remote call of some sort.

You can see evidence of this if you ask your browser to show the source code of the page. There you will not see the PHP code, that is because the PHP code is not send to the client, therefore it cannot be executed from the client. That's why you need to do a remote call to be able to have the client trigger the execution of PHP code.

If you don't use a form [as shown above] you can do that remote call from JavaScript with a little thing called Ajax. You may also want to consider if what you want to do in PHP can be done directly in JavaScript.

How to call another PHP file?

Use a form to do the call. You can have it to direct the user to a particlar file:


  

The user will end up in the page myphpfile.php. To make it work for the current page, set action to an empty string [which is what I did in the example I gave you early].

I just want to link it to a PHP file that will create the permanent blog post on the server so that when I reload the page, the post is still there.

You want to make an operation on the server, you should make your form have the fields you need [even if type="hidden" and use POST]:


  
  

What do I need to know about it to call a PHP file that will create a text file on a button press?

see: How to write into a file in PHP.

How do you recieve the data from the POST in the server?

I'm glad you ask... Since you are a newb begginer, I'll give you a little template you can follow:


 
 
   
     
     Page title
   
   
     
       
       
     
   
 

Note: you can remove var_dump, it is just for debugging purposes.

How do I...

I know the next stage, you will be asking how to:

  1. how to pass variables form a PHP file to another?
  2. how to remember the user / make a login?
  3. how to avoid that anoying message the appears when you reload the page?

There is a single answer for that: Sessions.

I'll give a more extensive template for Post-Redirect-Get




  
    
    Page title
  
  
    ;
    
      
      
    
  

Please look at php.net for any function call you don't recognize. Also - if you don't have already - get a good tutorial on HTML5.

Also, use UTF-8 because UTF-8!

Notes:

I'm making a simple blog site for myself and I've got the code for the site and the javascript that can take the post I write in a textarea and display it immediately.

If are you using a CMS [Codepress, Joomla, Drupal... etc]? That make put some contraints on how you got to do things.

Also, if you are using a framework, you should look at their documentation or ask at their forum/mailing list/discussion page/contact or try to ask the authors.

Ok... but how do I Use Ajax anyway?

Well... Ajax is made easy by some JavaScript libraries. Since you are a begginer, I'll recomend jQuery.

So, let's send something to the server via Ajax with jQuery, I'll use $.post instead of $.ajax for this example.




  
    
    Page title
    
        function ajaxmagic[]
        {
            $.post[                             //call the server
                "test.php",                     //At this url
                {
                    field: "value",
                    name: "John"
                }                               //And send this data to it
            ].done[                             //And when it's done
                function[data]
                {
                    $['#fromAjax'].html[data];  //Update here with the response
                }
            ];
        }
    
  
  
    
    
  

The above code will send a POST request to the page test.php.

Note: You can mix sessions with ajax and stuff if you want.

How do I...

  1. How do I connect to the database?
  2. How do I prevent SQL injection?
  3. Why shouldn't I use Mysql_* functions?

... for these or any other, please make another questions. That's too much for this one.

How do I code a PHP file in HTML?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP.

How does PHP work with HTML?

PHP processor scans the page, line by line. It build a processed HTML page. If it finds HTML, it passes that on as part of the processed HTML page it is building. If it finds PHP scripts, the PHP processor may or may not output HTML.

Chủ Đề