如何使用PHP和Javascript将数据从一个页面传递到另一个页面?

时间:2022-01-18 20:56:04

I have 3 buttons (image links, eventually will evolve to javascript or something better though)

我有3个按钮(图像链接,最终将演变为javascript或更好的东西)

Each of these buttons should send a value to a handler script, which does choice 1 if button 1 is pressed, choice 2, so on and so forth.

这些按钮中的每一个都应该向处理程序脚本发送一个值,如果按下按钮1,则执行选择1,选择2,依此类推。

I'm wondering what the best way to do this is. I DON'T WANT TO USE GET. I'd like to use POST if that's possible but I don't think it is. I thought about cookies too but the problem is even though you can call a JS function to create a cookie via a link, you can't go to the PHP page for processing, within the same click can you?

我想知道最好的办法是什么。我不想使用GET。如果可能的话我想使用POST,但我认为不是。我也考虑过cookie,但问题是即使你可以调用JS函数通过链接创建一个cookie,你也不能去PHP页面进行处理,在同一个点击中你可以吗?

It would work like the user clicks the button img, then it takes them to the handler script, but the handler redirects them back before they even know they were there.

它会像用户单击按钮img一样工作,然后将它们带到处理程序脚本,但处理程序会在它们知道它们存在之前重新定向它们。

Again this isn't a form, I need to do this with a hyperlink. I suppose I could just have a different page for each choice, but I don't think that's efficient.

再次,这不是一个表单,我需要用超链接做这个。我想我可以为每个选择设置不同的页面,但我认为这不是有效的。

Thanks!

4 个解决方案

#1


2  

If you want the variables to be passed using POST, you could create a form on the page, and have your links execute some javascript code at onClick. They'd set the variable to the desired value, then submit the form. The key lines would be something like:

如果您希望使用POST传递变量,您可以在页面上创建一个表单,并让您的链接在onClick上执行一些javascript代码。他们将变量设置为所需的值,然后提交表单。关键线将是这样的:

document.getElementById("user_choice").value = 2;   // or whatever the value for this link is
document.getElementById("my_form").submit();

#2


1  

You could turn your image links into:

您可以将图片链接转换为:

<form method="post" action="blah">
    <input type="hidden" name="function" value="function1" />
    <input type="image" scr="whatever" />
</form>

This way they still look like images, but are actually post forms with whatever you want inside of them. That's the easy way anyways. The harder way would be to use AJAX.

这样他们看起来仍然像图像,但实际上是在他们内部随意发布的形式。无论如何,这是简单的方法。更难的方法是使用AJAX。

#3


1  

In case you want to use JavaScript, have a look at jQuery. Using jQuery's click-method, you can easily handle clicks on elements:

如果您想使用JavaScript,请查看jQuery。使用jQuery的click方法,您可以轻松处理元素点击:

Suppose you have this HTML:

假设你有这个HTML:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Using jQuery, you can easily track click on the element labeled target:

使用jQuery,您可以轻松跟踪标记为target的元素:

$("#target").click(function() {
  alert("Handler for .click() called.");
});

In case you don't want to POST or GET clicks directly, I'd propose to register jQuery click-handlers, which set JavaScript variable based on the clicked element.

如果您不想直接POST或GET点击,我建议注册jQuery点击处理程序,它根据点击的元素设置JavaScript变量。

Note, that jQuery's click-handler can be registered with any element, not only forms. Furthermore note, that e.g. the above click-handler does not POST or GET the page to the server.

请注意,jQuery的click-handler可以注册任何元素,而不仅仅是表单。另外请注意,例如,上面的click-handler没有POST或GET页面到服务器。

Additionally, have a look here, on how to prevent the browser from submitting forms: What's the effect of adding 'return false' to a click event listener?

另外,请看一下如何阻止浏览器提交表单:将“return false”添加到click事件监听器有什么影响?

Even better than JavaScript is CoffeScript, which compile to JavaScript but makes live much easier. Give it a try!

比JavaScript更好的是CoffeScript,它可以编译成JavaScript但更容易实现。试一试!

#4


0  

what about AJAX? it's the best choice for your problem and the bonus part is you can use post too.

那AJAX呢?它是您问题的最佳选择,奖金部分也可以使用帖子。

#1


2  

If you want the variables to be passed using POST, you could create a form on the page, and have your links execute some javascript code at onClick. They'd set the variable to the desired value, then submit the form. The key lines would be something like:

如果您希望使用POST传递变量,您可以在页面上创建一个表单,并让您的链接在onClick上执行一些javascript代码。他们将变量设置为所需的值,然后提交表单。关键线将是这样的:

document.getElementById("user_choice").value = 2;   // or whatever the value for this link is
document.getElementById("my_form").submit();

#2


1  

You could turn your image links into:

您可以将图片链接转换为:

<form method="post" action="blah">
    <input type="hidden" name="function" value="function1" />
    <input type="image" scr="whatever" />
</form>

This way they still look like images, but are actually post forms with whatever you want inside of them. That's the easy way anyways. The harder way would be to use AJAX.

这样他们看起来仍然像图像,但实际上是在他们内部随意发布的形式。无论如何,这是简单的方法。更难的方法是使用AJAX。

#3


1  

In case you want to use JavaScript, have a look at jQuery. Using jQuery's click-method, you can easily handle clicks on elements:

如果您想使用JavaScript,请查看jQuery。使用jQuery的click方法,您可以轻松处理元素点击:

Suppose you have this HTML:

假设你有这个HTML:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

Using jQuery, you can easily track click on the element labeled target:

使用jQuery,您可以轻松跟踪标记为target的元素:

$("#target").click(function() {
  alert("Handler for .click() called.");
});

In case you don't want to POST or GET clicks directly, I'd propose to register jQuery click-handlers, which set JavaScript variable based on the clicked element.

如果您不想直接POST或GET点击,我建议注册jQuery点击处理程序,它根据点击的元素设置JavaScript变量。

Note, that jQuery's click-handler can be registered with any element, not only forms. Furthermore note, that e.g. the above click-handler does not POST or GET the page to the server.

请注意,jQuery的click-handler可以注册任何元素,而不仅仅是表单。另外请注意,例如,上面的click-handler没有POST或GET页面到服务器。

Additionally, have a look here, on how to prevent the browser from submitting forms: What's the effect of adding 'return false' to a click event listener?

另外,请看一下如何阻止浏览器提交表单:将“return false”添加到click事件监听器有什么影响?

Even better than JavaScript is CoffeScript, which compile to JavaScript but makes live much easier. Give it a try!

比JavaScript更好的是CoffeScript,它可以编译成JavaScript但更容易实现。试一试!

#4


0  

what about AJAX? it's the best choice for your problem and the bonus part is you can use post too.

那AJAX呢?它是您问题的最佳选择,奖金部分也可以使用帖子。