PHP初学者问题 [英] PHP Beginner question

查看:53
本文介绍了PHP初学者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个关于PHP编程理念的问题:


想象一下一个商店网站,其中有一个表格显示了一些

产品的名称。有一个链接购买每个产品旁边。我想要那个链接

来执行一些PHP代码,但我不希望导航器显示

另一个网页。


所以想象一下这个文件是product_list.php,链接是:< a

href =" add_product.php?code = ...>购买< / a>。我想要执行

add_product.php中的代码,但是我希望导航器显示当前显示的

相同的页面(product_list.php或

无论如何)。


我该怎么做?


谢谢!

解决方案

pe*****@gmail.com

嗨。我有一个关于PHP编程理念的问题:

想象一下一个商店的网站,其中有一个表格显示了一些
产品的名称。有一个链接购买每个产品旁边。我希望该链接执行一些PHP代码,但我不希望导航器显示另一个网页。

所以想象一下这个文件是product_list.php和链接是:< a
href =" add_product.php?code = ...>购买< / a>。我希望执行add_product.php中的代码,但我希望导航器显示当前显示的同一页面(product_list.php或
无论如何)。

我该怎么做?

谢谢!




有很多方法可以做到这一点,但最多常见的是:

1.让页面调用自己并在页面顶部添加php代码

来处理订单内容。

2.让页面调用另一个处理表单数据的php文件和

然后再次重定向到调用表单页面。


第一个样式有一个好处,所有代码都在一个地方,但

也有一个问题,即业务逻辑代码的数量可能会掩盖表单代码和更改外观和感觉形式可能很难。使用include()逻辑的各种''技巧'可能有助于此。


第二种风格将形式(即外观和感觉)与

分开业务逻辑通过将一组预定义的私有数据从表单传递到业务逻辑(通过POST或GET)和从业务逻辑到

表格(通过SESSION或GET)。这种分离是通常称为视图(用于表单)和控制器(用于

业务逻辑部分)的
。如果你添加一个数据库或其他数据库

(即数据''model''),那么你就更接近实现一个完整的
模型 - 视图 - 控制器( MVC2)实现。


两者都能很好地工作。选择最适合您需求的那个。


-david-


pepito3写道:

所以想象一下这个文件是product_list.php,链接是:< a
href =" add_product.php?code = ...>购买< / a>。我想要执行add_product.php中的代码,但我希望导航器显示当前显示的相同页面(product_list.php或
无论如何)。



Google forAJAX但请注意,浏览器支持不是100%。如果使用

AJAX,那么为那些不支持它(或禁用Javascript)的那些

浏览器启用常规旧链接是个好主意,听起来并不像听起来那么棘手。


-

Toby A Inkster BSc(Hons)ARCS

与我联系〜 http://tobyinkster.co.uk/contact


> 2.让页面调用另一个处理表单数据的php文件和

然后再次重定向到调用表单页面。




怎么能我让被调用文件确认调用文件?


ie:


*** product_list.php:

....

< a href =" add_product.php?code = ...>购买! < / a>

....


*** add_product.php:


< ?php

add_product(


Hi. I have a question about PHP programming philosophy:

Imagine a web of a shop with a table displaying the name of some
products. There is a link "Buy" next to each product. I want that link
to execute some PHP code, but I don''t want the navigator to show
another web.

So imagine the file is product_list.php and the link is: <a
href="add_product.php?code=...> Buy </a>. I want the code in
add_product.php to be executed but I want the navigator to display the
same page it is displaying at the moment (product_list.php or
whatever).

How can I do that?

Thanks!

解决方案

pe*****@gmail.com wrote:

Hi. I have a question about PHP programming philosophy:

Imagine a web of a shop with a table displaying the name of some
products. There is a link "Buy" next to each product. I want that link
to execute some PHP code, but I don''t want the navigator to show
another web.

So imagine the file is product_list.php and the link is: <a
href="add_product.php?code=...> Buy </a>. I want the code in
add_product.php to be executed but I want the navigator to display the
same page it is displaying at the moment (product_list.php or
whatever).

How can I do that?

Thanks!



There are a number of ways of doing this, but the most common ones would be:
1. Have the page call itself and add the php code at the top of the page
to handle the order stuff.
2. Have the page call another php file that processes the form data and
then redirects to the calling form page again.

The first style has the benefit that all the code is in one place but
also has the problem that the amount of business logic code may obscure
the form code and that changing the look and feel of the form may be
difficult. Various ''tricks'' with include() logic may help with this.

The second style separates the form (i.e. look and feel) from the
business logic by having a pre-defined set of private data that is
passed from the form to the business logic (via POST or GET) and from
the business logic to the form (via SESSION or GET). This separation is
often called the view (for the form piece) and the controller (for the
business logic piece). If you add a database or other data repository
(i.e. the data ''model''), then you are closer to implementing a full
model-view-controller (MVC2) implementation.

Both will work quite well. Choose the one that best suits your needs.

-david-


pepito3 wrote:

So imagine the file is product_list.php and the link is: <a
href="add_product.php?code=...> Buy </a>. I want the code in
add_product.php to be executed but I want the navigator to display the
same page it is displaying at the moment (product_list.php or
whatever).



Google for "AJAX" but be aware that browser support is not 100%. If using
AJAX, it''s a good idea to have the regular old links kick in for those
browsers that don''t support it (or have Javascript disabled), which isn''t
as tricky as it sounds.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact


> 2. Have the page call another php file that processes the form data and

then redirects to the calling form page again.



How can I make the called file to acknowledge the calling file?

i.e.:

*** product_list.php:
....
<a href="add_product.php?code=...> Buy! </a>
....

*** add_product.php:

<?php
add_product(


这篇关于PHP初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆