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:
<form action="">
<input type="submit" value="my button"/>
</form>
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.
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.
Use a form to do the call. You can have it to direct the user to a particlar file:
<form action="myphpfile.php">
<input type="submit" value="click on me!">
</form>
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
):
<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 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.
I'm glad you ask... Since you are a newb begginer, I'll give you a little template you can follow:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
//Ok we got a POST, probably from a FORM, read from $_POST.
var_dump($_PSOT); //Use this to see what info we got!
}
else
{
//You could assume you got a GET
var_dump($_GET); //Use this to see what info we got!
}
?>
<!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
, it is just for debugging purposes.
I know the next stage, you will be asking how to:
There is a single answer for that: Sessions.
I'll give a more extensive template for Post-Redirect-Get
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
var_dump($_PSOT);
//Do stuff...
//Write results to session
session_start();
$_SESSION['stuff'] = $something;
//You can store stuff such as the user ID, so you can remeember him.
//redirect:
header('Location: ', true, 303);
//The redirection will cause the browser to request with GET
//The results of the operation are in the session variable
//It has empty location because we are redirecting to the same page
//Otherwise use `header('Location: anotherpage.php', true, 303);`
exit();
}
else
{
//You could assume you got a GET
var_dump($_GET); //Use this to see what info we got!
//Get stuff from session
session_start();
if (array_key_exists('stuff', $_SESSION))
{
$something = $_SESSION['stuff'];
//we got stuff
//later use present the results of the operation to the user.
}
//clear stuff from session:
unset($_SESSION['stuff']);
//set headers
header('Content-Type: text/html; charset=utf-8');
//This header is telling the browser what are we sending.
//And it says we are sending HTML in UTF-8 encoding
}
?>
<!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 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.
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.
<?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( //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
}
);
}
</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 page test.php
.
Note: You can mix sessions
with ajax
and stuff if you want.
... for these or any other, please make another questions. That's too much for this one.