search
HomeBackend DevelopmentPHP Tutorial将 file_get_content() 返回的内容在 bootStrap 的模态框中显示

php 小白,想实现将 file_get_content() 读到的文本内容,显示在 bootstrap 的 模态框中,不懂怎么实现。

先说下,发帖前已经自己google百度了,没解决。最后聚焦在,如何点击  这个按钮以后,将这个参数 $p(其中$p = ../web/test.php) 传给 file_get_contment()后,再由 file_get_content() 返回的内容传给 模态框里面显示 。

点击这里的按钮,然后在模态框中查看




新人,知道代码有许多不规范,勿喷


回复讨论(解决方案)

请使用ajax:因为.php文件在Apache或者nginx进行解释的时候,会把里面的进行解释,然后翻译成一个html文件,输送到浏览器,用户在客户端点击button去触发file_get_contents是不可能的,除非你之前已经把file_get_contents的内容获取到了并且隐藏(display:none;),然后点击button再display:block;或者点击button的时候,发送ajax请求去获取file_get_contents的内容展示,当然刷新页面也可以。

请使用ajax:因为.php文件在Apache或者nginx进行解释的时候,会把里面的进行解释,然后翻译成一个html文件,输送到浏览器,用户在客户端点击button去触发file_get_contents是不可能的,除非你之前已经把file_get_contents的内容获取到了并且隐藏(display:none;),然后点击button再display:block;或者点击button的时候,发送ajax请求去获取file_get_contents的内容展示,当然刷新页面也可以。



在发帖前,试过ajax,把$p = "../web/index.php" 这个带路径文件名,传到后台后,用file_get_content()得到的内容,是否用 Json格式返回,之前试用,用json格式返回,没成功。所以问题在两点 ; 

在后台中 返回  
echo json_decode(file_get_content($p));


前台,ajax 设置dataType:'json'

也试过,没成

以上,感谢你的回答

这与 file_get_content() 没有直接的关系

1、既然页面使用的是 bootStrap,那么他就是用 ajax 与 服务端 交换信息的
你可将他发出去的内容直接返回,从而判定 bootStrap 中是否正常

2、判定 file_get_content() 结果是否正常
echo file_get_content(提交的路径)
只需在 php 中模拟实现就可以,与客户端无关

3、只在上两个检查都正确无误时,还需要检查数据格式是否正确

file_get_contents只是获取数据,但最后是需要js把获取的数据放入bootstrap。所以与file_get_contents无关。
应该看看返回的内容。

file_get_contents只是获取数据,但最后是需要js把获取的数据放入bootstrap。所以与file_get_contents无关。
应该看看返回的内容。



碰到个奇怪的小问题,ajax返回数据后,alert能显示出来,要是放在input标签里面,也有值,但是放在 div标签里面就不行了,不复杂,就几行代码,但是不懂为什么 div 的就不能显示内容

这是后台的


这是javascipt的函数


这是模态框里面的写法


这是运行结果




想知道,为什么 div 不能显示 ajax返回的内容

file_get_contents只是获取数据,但最后是需要js把获取的数据放入bootstrap。所以与file_get_contents无关。
应该看看返回的内容。



这与 file_get_content() 没有直接的关系

1、既然页面使用的是 bootStrap,那么他就是用 ajax 与 服务端 交换信息的
你可将他发出去的内容直接返回,从而判定 bootStrap 中是否正常

2、判定 file_get_content() 结果是否正常
echo file_get_content(提交的路径)
只需在 php 中模拟实现就可以,与客户端无关

3、只在上两个检查都正确无误时,还需要检查数据格式是否正确




看下5楼的一个小问题,ajax返回的数据不能显示,非常感谢

请使用ajax:因为.php文件在Apache或者nginx进行解释的时候,会把里面的进行解释,然后翻译成一个html文件,输送到浏览器,用户在客户端点击button去触发file_get_contents是不可能的,除非你之前已经把file_get_contents的内容获取到了并且隐藏(display:none;),然后点击button再display:block;或者点击button的时候,发送ajax请求去获取file_get_contents的内容展示,当然刷新页面也可以。



在继续进行的过程中,碰到一个小问题,在5楼,帮看下,非常感谢

因为从后台返回过来的是json数据啊,你用了$.ajax,而且指定了数据类型为json,当然会被解析为json对象。没错,是json对象,而不是字符串。
所以你要么改变指定的数据类型dataType:text或者dataType:html
要么把数据变为json字符串

JSON.stringify(msg)

你获取到的数据是 php 代码,用文本方式才能显示
否则浏览器会把 

php代码要放入 中,否则作为html代码是不会解析的。

做个总结,在后台返回数据,是json格式,在模态框里面,用 textarea这个标签就可以显示了。

感谢楼上的各位

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.