How to make PHP button call?
I don't care if the page reloads or displays the results right away;
Good!
Note. If you do not want to refresh the page, see "Okay ... but how can I use Ajax?" below.
I just want a button on my website to run a PHP file.
This can be done with a single button:
<form action=""> <input type="submit" value="my button"/> </form>
What is it.
To a large extent. Also note that there are times when ajax is really a way to go.
It depends on what you want. In general, you only need ajax if you want to avoid loading the page. However, you said that you do not care.
Why can't I call PHP directly from JavaScript?
If I can write the code inside the HTML just fine, why can't I just link to the file for it there or make a simple call to it in Javascript?
Since the PHP code is not in HTML just fine . This is an illusion created by the fact that most server-side scripting languages ββwork (including PHP, JSP, and ASP). This code exists only on the server, and it is not accessible to the client (browser) without any remote call.
This can be seen if you ask your browser to show the source code of the page. You will not see the PHP code there, because the PHP code is not sent to the client, so it cannot be executed with the client. To do this, you need to make a remote call in order to be able to initiate the client starting PHP code.
If you are not using a form (as shown above), you can make this remote JavaScript call using the little Ajax thing. You might also wonder if you can do what you want to do in PHP, in JavaScript.
How to call another PHP file?
Use the form to make a call. You can force him to direct the user to the member file:
<form action="myphpfile.php"> <input type="submit" value="click on me!"> </form>
The user will be on the page myphpfile.php . To make it work for the current page, set the action to an empty line (this is what I did in the example I gave you earlier).
I just want to link it to a PHP file that will create a permanent blog entry on the server so that when the page reloads, the message is still there.
You want to do the operation on the server, you must make your form the fields you need (even if type="hidden" and use POST ):
<form action="" method="POST"> <input type="text" value="default value, you can edit it" name="myfield"> <input type="submit" value = "post"> </form>
What do I need to know about this in order to call a PHP file that will create a text file with the click of a button?
see How to write a file in PHP .
How do you get data from POST on the server?
I'm glad you ask ... Since you're a newb begginer, I will give you a small template that you can use:
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { <!DOCTYPE html> <html lang="en"> <head> <meta char-set="utf-8"> <title>Page title</title> </head> <body> <form action="" method="POST"> <input type="text" value="default value, you can edit it" name="myfield"> <input type="submit" value = "post"> </form> </body> </html>
Note: you can remove var_dump , this is just for debugging purposes.
How do I...
I know the next step, you will be asking how:
- How to transfer variables from a PHP file to another?
- how to remember user / make login?
- How to avoid this anonymous message appears when the page reloads?
There is one answer for this: sessions.
I will give a more extensive template for Post-Redirect-Get
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { var_dump($_PSOT); <!DOCTYPE html> <html lang="en"> <head> <meta char-set="utf-8"> <title>Page title</title> </head> <body> <?php if (isset($something)){ echo '<span>'.$something.'</span>'}?>; <form action="" method="POST"> <input type="text" value="default value, you can edit it" name="myfield"> <input type="submit" value = "post"> </form> </body> </html>
Please look at php.net to call any function that you do not recognize. Also - if you haven't done it yet - get a good HTML5 tutorial.
Also, use UTF-8 because UTF-8 !
Notes
I make a simple blog site for myself, and I have code for the site and javascript that can take the post that I write in the text box and display it right away.
If you use CMS (Codepress, Joomla, Drupal ... etc.)? This puts some restrictions on how you should do something.
In addition, if you use the framework, you should look at their documentation or ask on your forum / mailing list / discussion page / contact or ask the authors.
Ok ... but how can I use Ajax?
Well ... Ajax is simplified by some JavaScript libraries. As you begin, I recommend jQuery .
So, send something to the server via Ajax using jQuery, I will use $. post instead of $. ajax for this example.
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { var_dump($_PSOT); header('Location: ', true, 303); exit(); } else { var_dump($_GET); header('Content-Type: text/html; charset=utf-8'); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta char-set="utf-8"> <title>Page title</title> <script> function ajaxmagic() { $.post( </script> </head> <body> <input type="button" value = "use ajax", onclick="ajaxmagic()"> <span id="fromAjax"></span> </body> </html>
The above code will send a POST request to the test.php page.
Note. You can mix sessions with ajax and more if you want.
How do I...
- How to connect to the database?
- How to prevent SQL injection?
- Why shouldn't I use the Mysql_ * functions?
... for these or any other questions, please ask another question. This is too much for this.