Question about php page creating
I want to create a simple website with registration and user profil开发者_Go百科es. Now, I don't really know is there a way for a user to create a new page when he clicks on, let's say, NEW POST or something like that. Stackoverflow is using something similar if I'm not wrong, so I would like someone to introduce me with how is this done.
BTW, I'm working under PHP + MySQL
I think you're looking for a template page with content from the db.
For example, stackoverflow keeps the page structure/template somewhere on their servers' file system. Every new page added is really just a few database entries. When somebody clicks on a page they're getting the template populated with the results of a query like "select * from pages where id=[id]".
Btw, I'm making assumptions that this is how stackoverflow works, I suppose they could theoretically have a new physical page for every question made...
The traditional way:
- Create a new PHP page that output an HTML form using
<FORM>
with an<INPUT type="submit">
button and several other<INPUT>
fields. - When the user clicks the button, data from the fields is posted to your form. Retrieve this data at the start of your PHP script using the
$_GET[]
or$POST[]
arrays. - Use the MySQL PHP API to save the data in a table in your database and retrieve it later in the same or another PHP page.
This method has the disadvantage that any time a button is clicked, the page has to be refreshed. Therefore, many websites these days use the AJAX way:
- Create a HTML page with a link element that links nowhere but triggers a JavaScript function, eg.
<A href="#" onClick="doSubmitData()">Click me</A>
. - In the JavaScript function, retrieve the data from the DOM and post it to a separate backend PHP script eg "savedata.php"
- Repeat last two steps above (retrieve data in PHP, save in MySQL)
- When you later need the data follow the opposite way, eg. use JavaScript to access something like "getdata.php" which retrieves your data in XML or JSON format, then parse that and display the data on elements in your DOM.
In the latter case I like to use jQuery, because it saves a lot of work on the Javascript side.
Welcome to a long, long journey in working with web applications. The best thing to do with what information you've provided is to start any of the great tutorials available on dynamic web site development. Set realistic goals for learning PHP programming (since that's the language you stated interest in) and understand at the outset that this is going to take a HUGE investment of time and effort to learn and do effectively.
But to give a basic rundown on what makes a dynamic site:
Dynamic websites (in the smaller context) and web applications (in the larger context) rely on some kind of persistence layer (usually a database like MySql) to store data and user input, since the basic HTTP environment is stateless and does not provide any provisions for remembering and maintaining user content.
So, some kind of 'display tier' presents a form that gathers user input, whether this be a login, editing a page, making a blog post, etc. This is sent via HTTP to the 'program tier' that sanitizes and secures this input and saves it down to a database or flat file. On retrieval, the program tier is also responsible for calling this data back up from its storage using any set of program specified identifiers (in databases these usually take the form of primary keys.)
Most of these kinds of applications will be built with some kind of framework that follows one of the well-known programming structures. One of the most common for web apps is called Model-View-Controller. This is because the way this architecture seperates these tiers closely resembles the flow of a typical user request in a web environment (view - interface for the user, controller - program tier, model - a programmed representation of the data linked up to the database to implement its properties).
Keep in mind this is all just the theory in a summary. You will very likely start out writing simple procedural (top to bottom scripts) programs that run basic commands as you increase your vocab of PHP. If you are seriously interested in learning this as a possible career or a vehemently pursued hobby, I suggest searching up some basic PHP CMS tutorials to get a feel for how the database and PHP work together. If you are just trying to get together a simple, one time content managed solution then I really must suggest you utilize some of the great open-source solutions available like Wordpress. Either way I wish you the best of luck in your pursuit!
It's a long journey, but for answering your question,
html-forms uses either POST or GET. Stick with post on any action that does more than GET data (avoid XSS-issues)
as a beginner, use numbers as input or learn http://bobby-tables.com/ (it's a db-thing, but just the same in example below...)
A simple but working example;
<html>
<body>
<form method="post" action="adduser.php">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="submit" value="create" />
</form>
</body>
</html>
adduser.php:
<?
//get form fields...
$username = str_replace(';','',$_POST['username'];
$password = str_replace(';','',$_POST['password'];
//insert into text-table...
$fp = fopen('usertable.dat','a');
fwrite($fp,$username . ';' . $password) . chr(13). chr(10);
?>
<html>
....
<p>Your account has been created.</p>
As you add validation, security, integrity, you'll end up with the need of a good framework. There are dozens. wordpress may be a good starting point.
Regards, //t
精彩评论