PHP在MVC中,动态创建链接

发布于 2024-12-03 12:49:12 字数 1205 浏览 3 评论 0原文

我的视图中有一些输入字段。我希望根据用户在这些字段中的输入动态生成链接。

我的理解是,通常情况下,页面渲染后无法更新 php,但由于某种原因,我认为这在 MVC 设置中是不同的?

我的解决方案 - 基于这个假设 - 是将 php 变量存储在用户输入字段中。并在生成链接的地方调用该变量。

例子: 输入字段,我想将输入文本存储到新变量 $locationhold

<dt><label for="location_area" class="required">Area</label></dt>
    <dd><input type="text" name="location_area" id="location_area" value="<?php if($this->validation->location_area){ echo $this->validation->location_area; $locationhold=$this->validation->location_area; }else echo $property->location_area; ?>" class="required" /></dd>

,然后以某种方式调用 $locationhold 并使其成为此链接的一部分:

<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.mysite.com/item/listing/<?php echo $locationhold ?>"  data-text="NEW POST!" data-count="none">Tweet</a>

无论如何,这并不完全是工作,提示? 被忽略。我的推文显示“新帖子!http://www.mysite.com/item/listing/”,而不是我想要动态附加到链接的其余信息。这可能是因为当页面呈现时, $locationhold 仍然为空,但是我如何保留其中的用户修改值并实现我正在寻找的结果

I have some input fields in my View. I want a link to be generated dynamically based off the user's input into these fields.

My understanding is that normally, php cannot be updated after the page is rendered, but for some reason I think this is different in an MVC setup??

My solution - based on this assumption - is to store a php variable in the user input field. And call that variable where my link is generated.

example:
the input field, I want to store the input text into new variable $locationhold

<dt><label for="location_area" class="required">Area</label></dt>
    <dd><input type="text" name="location_area" id="location_area" value="<?php if($this->validation->location_area){ echo $this->validation->location_area; $locationhold=$this->validation->location_area; }else echo $property->location_area; ?>" class="required" /></dd>

and then somehow summon $locationhold and make it part of this link:

<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.mysite.com/item/listing/<?php echo $locationhold ?>"  data-text="NEW POST!" data-count="none">Tweet</a>

anyway this doesn't quite work, tips? the <?php echo ?> is ignored. My tweet says "New Post! http://www.mysite.com/item/listing/" and not the rest of the information that I wanted to dynamically append to the link. This may be because by the time the page rendered, $locationhold was still empty, but how can I retain a user modified value in it and achieve the result I am looking for

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈场末日恋爱 2024-12-10 12:49:12

在传输到 Twitter 之前,您需要将用户输入发布到您的服务器。您可以将表单发布到服务器,在其中配置 locationhold,然后将您的请求配置为 twitter 并从服务器而不是客户端发送信息。

或者,您可以使用 ajax 在表单中完成输入后向服务器提交必要的信息,以计算出您的 locationhold,然后在用户将其提交到之前将配置的 URL 注入表单客户端叽叽喳喳。

You need to post the user input to you server before transmitting to twitter. You would have your form post to your server where you would configure the locationhold, then you would configure your request to twitter and send the information along from the server instead of the client.

Alternatively you could use ajax to submit the necessary info to figure out your locationhold to the server when the inputs are completed in the form, then inject the configured URL into the form client side before the user submits it to twitter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文