如何调用php中的数组
我在搜索框输入“bai”会自动提示“baidu”和“baidu image”,
点击相应的标签进入到相应的网站(比如点击"baidu",就打开http://baidu.com),html+js应经实现了,现在想用数组的方式把数据写在php里,这个怎么写啊,也就是这段
var mockData=[{name:'baidu',link:"http://www.baidu.com"},{name:'baidu image',link:"http://image.baidu.com"}]
怎么写在写在php里,并且被调用
<!DOCTYPE html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"/><title>如何数组查询</title><meta name=viewport content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/><style type="text/css">body{font-family:"yaHei Consolas Hybrid";margin:0}ul{margin:0;list-style:none;padding:0}.search-block{width:400px;position:relative;margin:50px auto}.search-block input{font-size:20px}.lbl-default{position:absolute;cursor:text;top:15px;left:15px;color:#ccc}.txt-default{width:378px;height:28px;border:1px solid #ddd;padding:10px}.btn{border:0;width:100px;height:50px;background-color:#56bdf3;color:#fff;text-align:center;cursor:pointer}.search-button{position:absolute;left:300px;top:0}.search-advice{display:none;width:400px;position:absolute;background-color:#fff}#search-advice-list{border-left:1px solid #ddd;border-right:1px solid #ddd;border-bottom:1px solid #ddd}#search-advice-list li{display:block;width:378px;height:30px;padding:10px}#search-advice-list li:hover{background-color:#f5f5f5}</style><script type="text/javascript">var mockData=[{name:'baidu',link:"http://www.baidu.com"},{name:'baidu image',link:"http://image.baidu.com"}];function $(id){return document.getElementById(id);}function hide(dom){dom.style.display='none';}function show(dom){dom.style.display='block';}function bindLabelToInput(labelId,inputId){$(inputId).onfocus=function(){hide($(labelId));};$(inputId).onblur=function(){if(this.value==null||this.value.length<1){show($(labelId));}};}function createDomTo(tagName,arr,parentNode){for(var i=0;i<arr.length;i++){var tag=document.createElement(tagName);tag.innerHTML="<a href='"+arr[i].link+"' target=_black>"+arr[i].name+"</a>";parentNode.appendChild(tag);}}window.onload=function(){bindLabelToInput('lbl-search','txt-search');var adviceWords=new Array();var ul=$('search-advice-list');$('txt-search').onkeyup=function(){if(this.value.length>0){for(var i=0;i<mockData.length;i++){if(mockData[i].name.indexOf(this.value)>=0){adviceWords.push(mockData[i]);}}if(adviceWords.length>0){ul.innerHTML='';createDomTo('li',adviceWords,ul);show(ul.parentNode);}adviceWords=new Array();}else{hide(ul.parentNode);}};};</script></head><body><div class=search-block><label for=txt-search id=lbl-search class=lbl-default>请输入要搜索的内容</label><input type=text id=txt-search class=txt-default /><input type=button id=submit-button class="btn search-button" value="搜索"/><div class=search-advice><ul id=search-advice-list></ul></div></div></body></html>
回复讨论(解决方案)
$ar = array( array('name' => 'baidu', 'link' => "http://www.baidu.com"), array('name' => 'baidu', 'link' => "http://www.baidu.com"), array('name' => 'baidu', 'link' => "http://www.baidu.com"),);$s = json_encode($ar);echo "var mockData = $s;";
<script src=mockData.php></script>
$ar = array( array('name' => 'baidu', 'link' => "http://www.baidu.com"), array('name' => 'baidu', 'link' => "http://www.baidu.com"), array('name' => 'baidu', 'link' => "http://www.baidu.com"),);$s = json_encode($ar);echo "var mockData = $s;";
<script src=mockData.php></script>
不行啊 无法调用
mockdata.php
index.html
<!DOCTYPE html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"/><title>如何数组查询</title><meta name=viewport content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/><style type="text/css">body{font-family:"yaHei Consolas Hybrid";margin:0}ul{margin:0;list-style:none;padding:0}.search-block{width:400px;position:relative;margin:50px auto}.search-block input{font-size:20px}.lbl-default{position:absolute;cursor:text;top:15px;left:15px;color:#ccc}.txt-default{width:378px;height:28px;border:1px solid #ddd;padding:10px}.btn{border:0;width:100px;height:50px;background-color:#56bdf3;color:#fff;text-align:center;cursor:pointer}.search-button{position:absolute;left:300px;top:0}.search-advice{display:none;width:400px;position:absolute;background-color:#fff}#search-advice-list{border-left:1px solid #ddd;border-right:1px solid #ddd;border-bottom:1px solid #ddd}#search-advice-list li{display:block;width:378px;height:30px;padding:10px}#search-advice-list li:hover{background-color:#f5f5f5}</style><script src="mockData.php" type="text/javascript">function $(id){return document.getElementById(id);}function hide(dom){dom.style.display='none';}function show(dom){dom.style.display='block';}function bindLabelToInput(labelId,inputId){$(inputId).onfocus=function(){hide($(labelId));};$(inputId).onblur=function(){if(this.value==null||this.value.length<1){show($(labelId));}};}function createDomTo(tagName,arr,parentNode){for(var i=0;i<arr.length;i++){var tag=document.createElement(tagName);tag.innerHTML="<a href='"+arr[i].link+"' target=_black>"+arr[i].name+"</a>";parentNode.appendChild(tag);}}window.onload=function(){bindLabelToInput('lbl-search','txt-search');var adviceWords=new Array();var ul=$('search-advice-list');$('txt-search').onkeyup=function(){if(this.value.length>0){for(var i=0;i<mockData.length;i++){if(mockData[i].name.indexOf(this.value)>=0){adviceWords.push(mockData[i]);}}if(adviceWords.length>0){ul.innerHTML='';createDomTo('li',adviceWords,ul);show(ul.parentNode);}adviceWords=new Array();}else{hide(ul.parentNode);}};};</script></head><body><div class=search-block><label for=txt-search id=lbl-search class=lbl-default>请输入要搜索的内容</label><input type=text id=txt-search class=txt-default /><input type=button id=submit-button class="btn search-button" value="搜索"/><div class=search-advice><ul id=search-advice-list></ul></div></div></body></html>
你这够可以的!
建议你咨询一下你的老师,弄清楚有 src 属性的和没有 src 属性的 script 标记的区别!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
