简体   繁体   English

通过链接在页面上加载新内容而不更改URL

[英]Loading new content on the page via a link without changing the URL

I am working on a social network website similar to facebook. 我正在类似Facebook的社交网站上工作。 But, I am facing a rather confusing stage in the programming. 但是,我在编程中面临一个相当混乱的阶段。

I am done with the register/login/logout pages/scripts, and you can view profiles with the www.mywebsite.com/profile.php. 注册/登录/登出页面/脚本已完成,您可以使用www.mywebsite.com/profile.php查看配置文件。

Now, I want to do what facebook does and allow users to click links while on their profile page (info, notes, photos) but never actually leave www.mywebsite.com/profile.php — just the appropriate content is printed to the screen. 现在,我想做Facebook要做的事情,并允许用户在个人资料页面上单击链接(信息,注释,照片),但实际上从不离开www.mywebsite.com/profile.php-仅在屏幕上打印适当的内容。

How is this done? 怎么做? I am not asking anyone to code this for me, just point me in the right direction! 我不是在要求任何人为我编写代码,只是向我指出正确的方向!

You can use Ajax for this purpose. 您可以为此目的使用Ajax。

Put the content that you want to replace in a div and using ajax replace that div and only send that content. 将要替换的内容放在div中,然后使用ajax替换该div,仅发送该内容。

That will have to be done via Javascript and Ajax. 那必须通过Javascript和Ajax完成。

A javascript function will fire when the link is clicked. 单击链接后,将触发JavaScript函数。 An ajax request is sent to the corresponding php script which sends back a response to your javascript function. 一个ajax请求被发送到相应的php脚本,该脚本将响应返回给您的javascript函数。 You then parse this response and place it on the screen. 然后,您解析此响应并将其放在屏幕上。

If you go that way, have a fallback option that does not rely on javascript as well in case a user has JS turned off. 如果您采用这种方式,那么在用户关闭JS的情况下,还有一个不依赖javascript的后备选项。

For this, you need the technique known as Ajax , which is short for asynchronous JavaScript and XML. 为此,您需要一种称为Ajax的技术,该技术是异步JavaScript和XML的简称。 The basic idea is that when the user does something - in your case clicks on a link or button - instead of loading a page, a script runs that calls on a server side script to send back some data. 基本思想是,当用户执行某项操作(在您的情况下,单击链接或按钮)而不是加载页面时,将运行脚本,该脚本调用服务器端脚本以发送回一些数据。 This is sometime XML, but you can get other types of data back as well. 这有时是XML,但您也可以获取其他类型的数据。 The asynchronous part is that the user and the page can go on doing other things while waiting on your script to return the data you asked for. 异步部分是,用户和页面可以在等待脚本返回所需数据的同时继续做其他事情。

There's a good book for beginners in Ajax that I read myself: Head First Ajax . 我读过一本适合Ajax初学者的好书: Head First Ajax Looks like you can pick up a used copy for about $10. 看起来您可以以10美元左右的价格拿起二手副本。 It's a nice intro, has a quirky style that appeals to some, and the authors do whatever they can to keep your attention. 这是一个不错的介绍,具有一些古怪的风格,吸引了一些读者,并且作者竭尽所能来吸引您的注意。 Hardcore programmers probably won't like this one, but I sense you're a little newer to the game and this may be a good read. 硬核程序员可能不会喜欢这一本书,但是我觉得您对这款游戏来说是新手,这可能是一本好书。 Otherwise, Google "learning Ajax" and there are a bajillion resources. 否则, 谷歌“学习Ajax”并且有不计其数的资源。

Good luck! 祝好运!

To respond to your comment, you can set up a "router" script that takes input and runs a specific function in response. 要回复您的评论,您可以设置一个“路由器”脚本,该脚本接受输入并运行特定的功能以进行响应。 This "router" function looks at the $_GET[] superglobal for a parameter like "action" and then calls a corresponding function. 此“路由器”函数在$ _GET []超全局变量中查找诸如“ action”之类的参数,然后调用相应的函数。 If not action parameter is sent over, the router calls a default function. 如果未发送操作参数,则路由器将调用默认功能。

Now for a little more detail. 现在更多细节。 Your page script would have 3 basic parts: The router, the various action functions, and the page template function. 您的页面脚本将包含3个基本部分:路由器,各种操作功能和页面模板功能。 The router just calls the appropriate function from the action functions and passes the output into the template function. 路由器只是从动作函数中调用适当的函数,然后将输出传递到模板函数中。 Here are a few examples. 这里有一些例子。

The user arrives on the page, index.php. 用户到达页面index.php。 No action is specified, so the router finds $_GET['action'] == '' and it calls default_action(). 没有指定动作,因此路由器找到$ _GET ['action'] ==''并调用default_action()。 This returns a welcome message, status, whatever, and the router passes this output to the function that displays your page, output included. 这将返回一个欢迎消息,状态等信息,路由器会将此输出传递给显示页面的功能,包括输出。

Now the user clicks a link/button for updates and arrives at index.php?action=update. 现在,用户单击链接/按钮进行更新,然后到达index.php?action = update。 $_GET['action'] == 'update', so the router calls update_action(). $ _GET ['action'] =='update',因此路由器调用update_action()。 The output goes on to the template function for display. 输出继续显示模板功能。

Does this help you envision how you might accomplish this? 这是否有助于您设想如何实现此目的?

You Can use this reference... 您可以使用此参考...

function showdiv(id) { if(id) { 函数showdiv(id){if(id){

var selected_offer="yourpagename.php"
HTML_AJAX.replace('divname',selected_offer);


}

} }

call showdiv on onChange() function of your link.. 在链接的onChange()函数上调用showdiv。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM