[英]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.