這是我的代碼,當我點擊添加按鈕新的文本字段即將到來,但整個頁面正在刷新和現有數據也刷新,請幫助我。如何使用PHP動態添加或刪除文本框?
<?php
if(!isset($_POST['j'])) {
static $j = 1;
} else {
//j is a reference for $i in the loop. $i will loop while it is less than $j.
if(isset($_POST['plus'])) {
$j = $_POST['j']+1; //by incrementing $j, $i will loop one more time.
}
if(isset($_POST['minus'])) {
if(@$j < 1) { //if there is only one box the user can't remove it
$j = $_POST['j']-1;
}
}
}
?>
<?php echo '<input type="hidden" name="j" value="' . @$j . '"/>' ?>
<?php for($i = 0; $i < @$j; $i++) { ?>
<input id="txt_id" name="txt_id[]" type="text" value="" readonly="readonly"/>
<? } ?>
<input type ="submit" value="+" name="plus">
<input type ="submit" value="-" name="minus">
設置會話附加$ J值,並嘗試it.or別人使用jQuery添加動態排它非常容易。 – Kathiravan
如果您想避免頁面刷新,您可以嘗試ajax並添加文本框字段 –
避免使用錯誤抑制'@'。只需檢查'$ j'是否使用[PHP isset()](http://php.net/manual/en/function.isset.php)設置 – PhearOfRayne