第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

是否可以將 HTML 輸入類存儲(chǔ)到 PHP 變量中?

是否可以將 HTML 輸入類存儲(chǔ)到 PHP 變量中?

PHP
慕無(wú)忌1623718 2021-06-28 13:53:21
我有一個(gè) html 表單和一些 php 以及一點(diǎn)點(diǎn) javascript。該表單有兩個(gè)輸入標(biāo)簽。兩個(gè)輸入標(biāo)簽都有類屬性。我想將類值“存儲(chǔ)”在 PHP 變量中,以便在單擊提交后回顯。我嘗試將 javascript 與第一個(gè) php 變量($firstclass)集成,但即使作為警報(bào)()也無(wú)法使其正常工作。我真的不想提醒類值,但認(rèn)為這將有助于找到解決方案。<form action="" method="post">    <input type="text" name="input1" class="hidden_class_1">    <input type="text" name="input2" class="hidden_class_2">    <input type="submit" name="submit"></form><?php$firstclass = ""; //hidden_class_1$secondclass = ""; //hidden_class_2$firstclass = "<script type=\"application/javascript\">alert(('this.className').attr('class'))</script>";$secondclass = ""; //ideally hidden_class_2if(isset($_POST['submit'])){    echo "<h2>First Input Class Value: ".$firstclass."</h2>";    echo "<h2>Second Input Class Value: ".$secondclass."</h2>";}我希望輸出如下;第一個(gè)輸入類值: hidden_class_1第二個(gè)輸入類值: hidden_class_2
查看完整描述

1 回答

?
蠱毒傳說(shuō)

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊

“最簡(jiǎn)單”的方法是使用 AJAX/XHR 并將類發(fā)送到 PHP 腳本。


<form id="ajaxform" action="path/to/script.php" method="post">

  <input type="text" name="input1" class="hidden_class_1">

  <input type="text" name="input2" class="hidden_class_2">

  <input type="submit" name="submit">

</form>

例如,使用 jQuery:


const $form = $('#ajaxform');


function onSuccess (response) {

  console.log('Successfully submitted the form');

  console.log('Server responded with', response);

}


function onFailure (jqXhr, status) {

  console.log('Ooops, something went wrong!');

  console.log('Server sent status code', status);

}


$form.on('submit', event => {

  event.preventDefault(); // suppress the reload


  const $input1 = $form.find('[name=input1]');

  const $input2 = $form.find('[name=input2]');


  $.ajax({

    method: $form.prop('method').toUpperCase(),

    url: $form.prop('action'),

    data: {

      input1Value: $input1.val(),

      input2Value: $input2.val(),

      input1Class: $input1.prop('className'),

      input2Class: $input2.prop('className')

    }

  }).

  done(onSuccess).

  fail(onFailure);

});

在您的 PHP 中,您將使用$_POST(或$_REQUEST) 來(lái)獲取已發(fā)送的值:


$input1_value = $_POST['input1Value'];

$input2_value = $_POST['input2Value'];

$input1_class = $_POST['input1Class'];

$input2_class = $_POST['input2Class'];


# do what you want with the variables

請(qǐng)注意,您必須在onSuccess函數(shù)內(nèi)部處理服務(wù)器的響應(yīng)。通常,人們使用 JSON 對(duì)來(lái)自服務(wù)器的響應(yīng)進(jìn)行建模。您可以使用 PHP 的內(nèi)置函數(shù)json_encode和json_decode函數(shù)。例如,您的 PHP 腳本可以回答:


$input1_value = $_POST['input1Value'];

$input2_value = $_POST['input2Value'];

$input1_class = $_POST['input1Class'];

$input2_class = $_POST['input2Class'];


# do what you want to do with the variables, then


$response = array(

  'ok' => true,

  'message' => 'PHP says "Thanks" for the information'

);


header('Content-Type: application/json');

echo json_encode($response);

die;

在onSuccess函數(shù)內(nèi)部,您將例如:


function onSuccess (response) {

  if (response.ok) {

    console.log('Submitted, and all values where OK');

    console.log(response.message);

    return; // opt-out early, no need for "else" keyword

  }

  console.log('Submitted, but something went wrong');

  console.log(response.message);

}


查看完整回答
反對(duì) 回復(fù) 2021-07-02
  • 1 回答
  • 0 關(guān)注
  • 134 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)