简体   繁体   English

提交表单并使用值(JavaScript)

[英]Submitting forms and using the value (JavaScript)

How do you use a value "submitted" by a form in javascript? 您如何使用javascript表单“提交”的值?

Facts: 事实:
It is a PHP document 这是一个PHP文档
I'm using JavaScript because I need some timing factors I don't think I can get from serverside-scripts :) 我使用JavaScript是因为我需要一些时序因素,但我认为我无法从serverside-scripts获得:)

To simplify; 为了简化 what I want is, that when this form is submitted or a button is clicked: 我想要的是,在提交此表单或单击按钮时:

<form method="POST" action="test.php">
<input type="text" name="foo" size="30">
<input type="submit" value="Click me"> //it doesn't have to be submitted
<input type="button" action="some_action" value="Click me"> //an alternative solution
</form>

the value of the text-input named "foo" is displayed elsewhere. 名为“ foo”的文本输入的值显示在其他位置。

NOTE The form doesn't have to be submitted, what I realy want is, that when you press a button the value can be used elsewhere 注意表单不必提交,我真正想要的是,当您按下按钮时,该值可以在其他地方使用

Should I use GET instead? 我应该改用GET吗? Can I just use the $_POST array? 我可以只使用$ _POST数组吗? Should I use AJAX (which I am completely useless at)? 我应该使用AJAX(我完全没有用)吗? I don't know what to do in this situation. 在这种情况下,我不知道该怎么办。

You can read the value from the $_POST or $_REQUEST array on the server side, and insert it into the output anywhere you like - even inside javascript, if you want to. 您可以从服务器端的$_POST$_REQUEST数组中读取值,然后将其插入到您喜欢的任何位置的输出中,即使您愿意,也可以将其插入到javascript中。 Example: 例:

<?php
$myValue = $_POST['foo'];
?>
<script type="text/javascript"><!--
function writeMyValue() { document.write('<?php echo $myValue; ?>'); }
// --></script>

Since you mentioned that it does not depend fully upon whether the form is submitted or not, so it's more easier to catch the value w/o POSTing / GETing the form. 由于您提到它并不完全取决于是否提交表单,因此在没有POST / GET表单的情况下获取值更加容易。 After you have written your interface logic in the body section, you need to write the following code in the footer page at the end:- 在主体部分中编写接口逻辑后,您需要在页脚末尾编写以下代码:-

anypage.php :- anypage.php :-

<form method="POST" action="test.php">
  <input type="text" name="foo" id="foo" size="30" />
  <input type="submit" onclick="return writeFoo('foo_placeholder', 'foo');" value="Click me" /> //it doesn't have to be submitted
  <input type="button" onclick="return writeFoo('foo_placeholder', 'foo');" action="some_action" value="Click me" /> //an alternative solution
</form>

The above code is your code only with some minor modifications, including calling a JS function " writeFoo() " on the "click" event of either a button / submit. 上面的代码仅对您的代码进行了一些小的修改,包括在按钮/提交的“ click”事件上调用JS函数“ writeFoo() ”。 This function takes 2 arguments:- 该函数有两个参数:

  • arg - It mentions the destination placeholder ID of the HTML element, in which the value is to be printed. arg提到要在其中打印值的HTML元素的目标占位符ID。
  • source - It mentions the source ID of the HTML element, from which the value is to be grabbed / taken. source -它提到的HTML元素,从该值是被抓取/截取的源ID。

rightpart.php :- rightpart.php :-

<div>
    <span id="foo_placeholder"></span>
</div>

The above HTML code can be used for any panel, but must be included when the "anypage.php" page is to be shown to the user. 上面的HTML代码可用于任何面板,但在向用户显示“ anypage.php”页面时必须将其包括在内。 This is because the placeholder element must be present when the "foo" element is being called. 这是因为在调用“ foo”元素时必须存在占位符元素。 Be careful to use the same ID both in the " writeFoo() " function calling time & in this page. 请注意在此页面的调用时间&的“ writeFoo() ”函数中都使用相同的ID。

footer.php :- footer.php :-

<script type="text/javascript"><!--
function writeFoo(arg, source) {
    if(document.getElementById(arg) != null) {
        document.getElementById(arg).innerHTML = document.getElementById(source).value;
    }
}
// --></script>

And this page should contain the above JS code containing the definition of the " writeFoo() " function. 并且此页面应包含上面的JS代码,其中包含“ writeFoo() ”函数的定义。

EDIT, as for @Latze:- 编辑,至于@Latze:-
See you can include that " rightpart.php " page either in the same block of " anypage.php " page or in any block of any other page (like " header.php " / " footer.php " page). 看看你能包括“ rightpart.php ”页无论是在“同一块anypage.php ”页面或任何其他页面(如“任何块header.php ” /“ footer.php ”页)。 But the main logic is that both the source ID (from which the value is taken) & the target / placeholder ID (where the value is to be shown) must be present when you are viewing that particular page (in this case, it means when you are viewing the " anypage.php " page). 但是主要的逻辑是,当您查看特定页面时,必须同时显示源ID(从中获取值)和目标/占位符ID(在其中显示值)(在这种情况下,这意味着当您查看“ anypage.php ”页面时)。

Hope it helps. 希望能帮助到你。

PHP runs on the server. PHP在服务器上运行。 JavaScript runs on the browser. JavaScript在浏览器上运行。 These two languages do not talk to each other; 这两种语言互不交谈; they don't even run at the same time, not to mention on the same machine. 它们甚至不会同时运行,更不用说在同一台机器上运行了。 As soon as the user submits the form, the browser requests test.php from the server and the current page is gone forever, scripts and all. 一旦用户提交了表单,浏览器就会从服务器请求test.php ,并且当前页面,脚本和所有内容都将永远消失。

It's really hard to figure out what you want to do exactly, so I'll provide you with some general hints: 真的很难弄清楚您想做什么,因此我将向您提供一些一般性提示:

  • JavaScript can intercept a form submission. JavaScript可以拦截表单提交。 You need to attach an onsubmit event handler to the <form> element. 您需要将onsubmit事件处理程序附加到<form>元素。 The function assigned to the event can do whatever it needs and then return true (and let the submission go on) or return false (and cancel the submission). 分配给该事件的函数可以执行所需的任何操作,然后返回true (使提交继续进行)或返回false (并取消提交)。

  • JavaScript can read and write almost any page element. JavaScript可以读写几乎任何页面元素。 You need to use the so called DOM methods. 您需要使用所谓的DOM方法。

  • PHP can generate whatever you need, including HTML input fields. PHP可以生成您需要的任何内容,包括HTML输入字段。

Example: 例:

<?php

$foo_value = date('Y-m-d H:i:s');

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<script type="text/javascript"><!--
window.onload = function(){
    var documentForms = document.getElementsByTagName("form");
    for(var i=0, len=documentForms.length; i<len; i++){
        documentForms[i].onsubmit = function(e){
            var currentForm = e.target;
            var fooValue = currentForm.elements.foo.value;

            var p = document.createElement("p");
            p.appendChild(document.createTextNode("Aborted submission: " + fooValue));
            currentForm.appendChild(p);

            return false;
        };
    }
};
//--></script>
</head>
<body>

<form method="POST" action="test.php">
<input type="text" name="foo" value="<?php echo htmlspecialchars($foo_value) ?>" size="30">
<input type="submit" value="Tryk her">
</form>

</body>
</html>

Update 更新

A little note about this: 关于此的一点说明:

documentForms[i].onsubmit = function(e){
};

When you assign an event handler, the spec requires that whenever the function gets called it will receive an event object as its first argument. 分配事件处理程序时,规范要求,每当调用该函数时,它将接收一个事件对象作为其第一个参数。 That object represents the event that triggered the function call and it can be used to obtain additional information, such as the original DOM node that triggered the event . 该对象代表触发函数调用的事件,它可用于获取其他信息,例如触发事件的原始DOM节点 It doesn't matter how you call it inside your function; 在函数中如何调用都无关紧要; I use e because I never know how to name stuff :) 我用e是因为我永远不知道该如何命名:)

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

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