How to call php file into a div?

2019-04-13 11:08发布

I'm trying to reload only specific div

$("#captcha").html('<?php echo 'abc';?>'); // just as test - works well

Because div content is much larger, I tried:

$("#captcha").html('<?php include 'myFile.php';?>'); // doesn't work

How can I call the code from a .php file into a div ?

5条回答
Lonely孤独者°
2楼-- · 2019-04-13 11:39

PHP is Preprocessed (PHP: Hypertext Preprocessor), and cannot be used without a new request. You need to use load to load the results of a php file into your javascript. Once the page is loaded, more PHP cannot be run without additional AJAX requests (or other methods that make new requests).

PHP is server-side, meaning that software (PHP) is installed on the server to interpret it before the response is sent. An HTTP request is a 'once-and-done' operation, meaning that once a response is sent, the connection between client and server is closed.

Think of PHP and other server-side languages as chefs in a kitchen. Say you order food for pickup, you tell the restaurant what you want and leave to pick it up. You get back to your house and your friend has decided that he wants something from them too. You didn't bring the chefs back with you, only the food they made - so the only way to get your friend's order is to drive all the way back and pick it up again.

I'm sure the above is a grammatical mess (its early), but I hope I got the point accross. Anyways, you will want to use .load to load the php file:

$("captcha").load('myfile.php');

load will send an AJAX request to the page myfile.php

查看更多
贼婆χ
3楼-- · 2019-04-13 11:41

You can try the load method in jquery

Its like

$('#captcha').load('myFile.php')

查看更多
Rolldiameter
4楼-- · 2019-04-13 11:48

Load it to a variable, then sanitize characters. I guess you may have some special chars there that breaks your javascript.

查看更多
甜甜的少女心
5楼-- · 2019-04-13 11:49

Did you tried loading the php via ajax? http://api.jquery.com/load/ in case you are working with jquery

查看更多
相关推荐>>
6楼-- · 2019-04-13 12:00

use $('div').load('phpfile.php')

$.load is used to load data from the server via an html file or a server-side script like php. Just to have an overview of what you can do with $.load you can also load page fragments by including a selector after the page you want to retrieve. This way only the contents of the selected element will be loaded into your container. You can also pass in data and execute functions after the request has complete(callbacks).

$.html is different because its only used to get or set the contents of an existing element. You can however use it with ajax methods like $.post, $.get, $.ajax to update the contents of an existing element.

Read up jQuery's documentation on the $.load and $.html methods if you want to have an in-depth understanding on how these method works.

查看更多
登录 后发表回答