Javascript-jquery ajax and php are used in combination to solve the problem of refreshing new forms.

Source: Internet
Author: User
This is my page. after opening the homepage link, the main body section is displayed (black box on the left side); the sub-page on the right side (the sub-page obtained through ajax in the right div # main_box) when the homepage link is opened, the subpage loaded when the button is pressed is displayed. now, the subpage 2 loaded when the button is pressed...

It is my page. after opening the home page link, the main part is displayed (black box on the left );
Subpage on the right (subpage obtained through ajax in the right p # main_box)
The subpage that is loaded when the button is clicked is displayed when the home page is opened.

Now there is a form in Subpage 2 that I load when I press button 2. I need to submit the form and get the value submitted by the form through PHP, and then query the information in the database, the returned database data is then output to this subpage 2. I don't know how to implement it.

This method has been found on the Internet for a long time:

Script $ (function () {$ ("# jiansuo "). click (function () {var xueqi = $ ("# xueqi "). val (); // Obtain the form value var one = $ ("# one "). val (); var one_week = $ ("# one_week "). val (); var more = $ ("# more "). val (); var more_week_num = $ ("# more_week_num "). val (); var more_week_ba =$ ("# more_week_ba "). val (); var xingqi = $ ("# xingqi "). val (); var banji =$ ("# banji "). val (); // Ajax post data $. ajax ({type: "POST", // The Ajax request is post url: "teac Her. php? C = fankui ", // ajax request URL script address data: {" xueqi ": xueqi," one ": one," one_week ": one_week," more ": more, "more_week_ba": more_week_ba, "xingqi": xingqi, "banji": banji}, // passed value success: function (data) {// callback function the data here can be returned in HTML or JSON for the sake of simplicity. $ ("# Main_right_box" ).html ("" ).html (data); // Insert the returned list into the category.}) ;});} script

But it won't be used. please enlighten me

Reply content:

It is my page. after opening the home page link, the main part is displayed (black box on the left );
Subpage on the right (subpage obtained through ajax in the right p # main_box)
The subpage that is loaded when the button is clicked is displayed when the home page is opened.

Now there is a form in Subpage 2 that I load when I press button 2. I need to submit the form and get the value submitted by the form through PHP, and then query the information in the database, the returned database data is then output to this subpage 2. I don't know how to implement it.

This method has been found on the Internet for a long time:

Script $ (function () {$ ("# jiansuo "). click (function () {var xueqi = $ ("# xueqi "). val (); // Obtain the form value var one = $ ("# one "). val (); var one_week = $ ("# one_week "). val (); var more = $ ("# more "). val (); var more_week_num = $ ("# more_week_num "). val (); var more_week_ba =$ ("# more_week_ba "). val (); var xingqi = $ ("# xingqi "). val (); var banji =$ ("# banji "). val (); // Ajax post data $. ajax ({type: "POST", // The Ajax request is post url: "teac Her. php? C = fankui ", // ajax request URL script address data: {" xueqi ": xueqi," one ": one," one_week ": one_week," more ": more, "more_week_ba": more_week_ba, "xingqi": xingqi, "banji": banji}, // passed value success: function (data) {// callback function the data here can be returned in HTML or JSON for the sake of simplicity. $ ("# Main_right_box" ).html ("" ).html (data); // Insert the returned list into the category.}) ;});} script

But it won't be used. please enlighten me

  1. Using the iframe function, the button 2 element is a tag, which is displayed as the desired button and adds the target attribute. in this way, your second requirement will naturally be solved.

    Button 2

  2. Add the click event to the button 2 tag, get the content to be displayed in main_box through ajax, add it to main_box, and then bind it to the submit event on the retrieved form, insert the form submitted response to main_box.

My practice is to disable default event submission, send requests using jq ajax, and use data for form data:
$ (FormSelector). serialize () does not need to read values separately. the page is refreshed by the callback function.

I know, let's say that ajax actually returns your teacher. for example, the dataType format is html. php performs a test and directly echo 'Hi, I am going to be passed back '. then, the result is returned in success: function (data) {alert (data )}; // check what it is. if it is json, it is teacher. php can only output echo json encode ($ array); dataType: 'json'
// Do I know if I have written an error? = #
// By the way, you have to add onsubmit = 'return checnkForm () 'for that form; write ajax in the checkForm () function, and finally return false, otherwise, a jump will occur when the form is submitted.

$. Ajax ({url: "teacher. php? C = fankui ", type:" post ", data: {" xx ":" xx "}, dataType: 'html', success: function (data) {// data loading successful}, error: function () {// data loading failed }});

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.