![](/img/trans.png)
[英]How to take an input and when the submit button is pressed put it into a variable?
[英]how to take a Value from a javascript variable, to be used as a php input variable via a button, without or not a submit button?
如何从一个javascript变量获取一个值,通过一个按钮而不是一个提交按钮用作一个php输入变量,因为提交按钮不能同时生成多个序列? 我尝试过这种方法,但我仍然无法使用按钮从js获取输出(“x”)。 这是我的第一个使用javascript的代码我尽力尽力,因为我试图使用break但它没有用,然后我把x--放开它。
这个javascript工作就像我想要的,但仍然无法从按钮值获得值“x”
码:
<title>Test-2019</title>
<!-- x += 1 -->
<!-- Java Script -->
<script>
x = 0;
function f_Next()
{
x++;
document.getElementById("Next").value = x;
if(x == 5) {
x--;
}
}
</script>
<!-- HTML -->
<form method="post" action="#">
<input type="button" value="Soal" id="Next" onClick="f_Next()">
<!--
<input type="hidden" value="0" id="Next">
-->
</form>
<!-- PHP -->
<?
$a = array("str1", "str2", "str3", "str4", "str5");
if (isset($_POST['soal']))
{
$va = $_POST['soal'];
print $a[$va];
}
?>
在delphi上我总是使用这个方法,我也尝试实现它,但它没有用,所以我尝试用java脚本。
德尔福示例代码:
//global var
const
a : array[0..4] of string = ('str1', 'str2', 'str3', 'str4', 'str5');
var
g : integer;
...
...
//on create
g := 0;
...
...
//on button click
g := g + 1;
if g = 5 then
g = 0;
edit1.text := IntToStr(g);
//or like this example 2
button1.tag := button1.tag + 1;
if button1.tag = 5 then
button1.tag := 0;
edit1.text := IntToStr(button1.tag);
PHP代码是我试过的,像这样
function fNext();
{
$x = 0;
if ($x == 5)
{
break;
}
x += 1;
}
对不起,如果我包括pascal / delphi代码,cz也许你可以帮我转换成php。
谢谢。
不确定你在这里尝试做什么,但是如果你想要一个<input>
元素的值到达PHP它有一个name
属性!
所以将输入更改为
<input name="Next" type="button" value="Soal" id="Next" onClick="f_Next()">`
// ^^^^^^^^^^^ added this
现在你需要在$_POST
数组中正确地解决它,所以如果我们在name="Next"
调用元素Next
,我们应该使用$_POST['Next']
而不是$_POST['soal']
作为soal
是value
属性,其中javascript应该用1或2等覆盖
$a = array("str1", "str2", "str3", "str4", "str5");
if (isset($_POST['Next']))
{
$va = $_POST['Next'];
print $a[$va];
}
我不是百分百肯定我抓住了问题的意图,但我希望这可能或多或少是预期的结果?
每次单击该按钮都会递增计数器x
,并将此值发送到PHP脚本。 PHP脚本使用适合x
值的数组中的值进行响应
ajax回调会增加变量并执行其他非常有趣的事情。
<?php
$a = array("str1", "str2", "str3", "str4", "str5");
if (isset($_POST['soal'])){
$va = $_POST['soal'];
$response=( array_key_exists( $va, $a ) ) ? $a[ $va ] : 'error';
exit( $response );
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test-2019</title>
<script>
document.addEventListener('DOMContentLoaded', ()=>{
let x=0;
const ajax=function(payload,callback){
let xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if( this.status==200 && this.readyState==4 )callback.call( this, this.response );
}
xhr.open('POST', location.href, true );
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send( payload );
}
const callback=function(r){
alert(r);
x++;
if( x==5 )x=0;
}
document.querySelector( 'form input[ type="button" ]' ).addEventListener('click', function(e){
ajax.call( this, 'soal=' + x, callback )
});
});
</script>
</head>
<body>
<form method='post'>
<input type='button' value='Soal' />
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.