Php + ajax implements a refreshing news and message system (with source code)

Source: Internet
Author: User

Ajax is very simple and easy to implement since jquery is available. Next I will introduce the implementation process of a new news message system based on jquery ajax + php mysql, I hope this article will help you.

The most concise and easy-to-understand ajax without refreshing message system. The Source Code ignores the data verification process. Readers can expand according to their own needs.


Core source code:


1. configuration file: config. php

The Code is as follows: Copy code


<? Php

// Database configuration information (username, password, database name, table prefix, etc)

$ Pai_dbhost = "localhost ";

$ Pai_dbuser = "root ";

$ Pai_dbpwd = "root ";

$ Pai_dbname = "ajaxdemo1 ";

$ Pai_dbprefix = "";

 


$ Link = mysql_connect ($ pai_dbhost, $ pai_dbuser, $ pai_dbpwd );

Mysql_select_db ($ pai_dbname );

Mysql_query ("set names utf8 ");

?>

2. Request Processing: deal. php

The Code is as follows: Copy code


<? Php

Header ("Content-type: text/html; charset = UTF-8 ");

Include "config. php ";

// Post receives data, which only demonstrates the effect. Verification is skipped here.

$ Name = $ _ POST ['name'];

$ Content = $ _ POST ['content'];

 


$ SQL = "insert into test (name, content) values ('{$ name}', '{$ content }');";

$ Res = mysql_query ($ SQL, $ link );

If ($ res ){

Echo '{"name": "'. $ name. '", "content": "'. $ content. '", "status": "1 "}';

}

?>

3. Homepage code: index. php

The Code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> refreshing </title>

<Link href = "css/css.css" type = "text/css" rel = "stylesheet"/>

<Style type = "text/css">

Body {color: #555; font-size: 14px; padding: 0; margin: 0 ;}

# Form {background: # dedede; padding: 10px 20px; width: 300px ;}

# Show {background: # f6f6f6; padding: 10px 20px; width: 300px ;}

# Show p {margin: 6px; font-size: 13px; line-height: 22px; border-bottom: 1px dashed # cdcd ;}

</Style>

<Script type = "text/javascript" src = "jquery-1.7.2.min.js"> </script>

<Script type = "text/javascript">

$ (Function (){

$ ("# Sub"). click (function (){

// Just describe the principle, and then the steps for verifying the content of the text box are saved here, And the ajax request is sent directly.

$. Post ("deal. php ", {name: $ (" # name "). val (), content: $ ("# content "). val ()}, function (data ){

If (data. status ){

Var str = "<p> <strong>" + data. name + "</strong> published:" + data. content + "</p> ";

$ ("# Show"). prepend (str); // append the prefix

} Else {

Alert ("comment failed ");

}

}, 'Json ');

});

});

</Script>

</Head>

 


<Body>

<Div id = "form">

<Form action = "deal. php" method = "get" id = "suggest_form">

Username: <input type = "text" name = "name" id = "name"/> <br/>

Content: <textarea name = "content" id = "content"> </textarea> & nbsp;

<Input type = "button" value = "publish" id = "sub"/>

</Form>

</Div>

<Div id = "show">

<? Php

Include "config. php ";

$ SQL = "select * from test ;";

$ Res = mysql_query ($ SQL, $ link );

While ($ row = mysql_fetch_array ($ res )){

Echo "<p> <strong> ". $ row ['name']. "</strong> posted :". $ row ['content']. "</p> ";

}

?>

</Div>

</Body>

</Html>

Database Files

The Code is as follows: Copy code


Drop table if exists 'test ';
Create table 'test '(
'Id' int (10) unsigned not null AUTO_INCREMENT,
'Name' varchar (64) not null,
'Content' text not null,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8;

Source code: php + ajax for refreshing news and message system downloads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.