2017-07-04 119 views
2

我有一個表格:如何將Jquery的值傳遞給PHP? HTML是一個隱藏的輸入表單字段

<form action="post.php" method="POST"> 
<p class="select1">Northern Cape</p> 
<p class="select1">Eastern Cape</p> 

<p class="select2" >New Goods</p> 
<p class="select2" >Used Goods</p> 

<input id="submt" type="submit" name="submit" value="Submit"> 
</form> 

JQUERY ....追加輸入/值對所選項目:

$(document).ready(function() { 
    $('.select1').click(function() {  
     if ($(this).text() == "Northern Cape"){ 
      $(this).append("<input id='firstloc' type='hidden' name='province' 
      value='Northern Cape' />"); 
      $("#firstloc").val('Northern Cape'); 
    }; // end of if statement 

     if ($(this).text() == "Eastern Cape"){ 
      $(this).append("<input id='firstloc' type='hidden' name='province' 
      value='Eastern Cape' />"); 
      $("#firstloc").val('Eastern Cape'); 
    }; // end of if statement 
}); // end of click function 
}); // end of document ready 

$(document).ready(function() { 
    $('.select2').click(function() { 
     if ($(this).text() == "New Goods"){ 
      $(this).append("<input id='category' type='hidden' name='cat' 
      value='New Goods' />"); 
      $(this).val('New Goods'); 
     }; 

     if ($(this).text() == "Used Goods"){ 
      $(this).append("<input id='category' type='hidden' name='cat' 
      value='Used Goods' />"); 
      $("#category").val('Used Goods'); 
     }; 

}); 
}); 

如何值傳遞給PHP即。第一個值省第二個值類別?

<?php 

$province = $_POST['province']; 
$category = $_POST['cat']; 
echo $province; 
echo $category; 
?> 

我收到一條消息未定義的索引:傳遞給PHP時的cat。 用戶必須選擇2項和值必須傳遞給PHP,我不想使用帶有「選項」的下拉菜單

+0

表單提交它會通過,你沒有得到它? – Exprator

+0

我得到未定義的索引:cat – ticktock

+0

選擇一個值後嘗試查看源代碼以檢查輸入字段是否添加到html頁面 – Exprator

回答

1

PHP沒有爲所有的post操作設置$ _POST,例如text/xml:

嘗試添加應用程序/ x-www-form-urlencoded或multipart/form-data到表單中以確保。

<form action="post.php" method="POST" enctype="multipart/form-data"> 

此外,請確保您沒有破碎的HTML導致您的表單標籤過早關閉。使用Web開發人員工具驗證要發佈的內容和內容類型。

你也應該使用isset檢查變量的存在:

if (isset($_POST["cat"])) { 
    $cat = $_POST["cat"]; 
    echo $cat; 
    echo " is the category"; 
} 
else 
{ 
    $cat = null; 
    echo "no category supplied"; 
} 
+0

謝謝,那是做的。這麼多想法和一個人想念小事情 – ticktock

2

這裏是解決方案。

您在代碼中犯了幾個錯誤。

  • 您的if語句
  • 你因此未接近$document.ready標籤正確
  • 您的post.php檢查,如果發佈的數據或不
  • 而你只追加input type hidden你沒有刪除後使用;它。

post.php中

<?php 
$province = ''; 
$category = ''; 
if(isset($_POST['province'])): 
    $province = $_POST['province']; 
endif; 
if(isset($_POST['cat'])): 
    $category = $_POST['cat']; 
endif; 
echo $province; 
echo $category; 
?> 

$(document).ready(function() { 
 
    $('.select1').click(function() {  
 
    if ($(this).text() == "Northern Cape"){ 
 
     $("#firstloc").remove(); \t 
 
     $(this).append("<input id='firstloc' type='hidden' name='province' value='Northern Cape' />"); 
 
    } // end of if statement 
 

 
    if ($(this).text() == "Eastern Cape"){ 
 
     $("#firstloc").remove(); \t 
 
     $(this).append("<input id='firstloc' type='hidden' name='province' value='Eastern Cape' />"); 
 
    } // end of if statement 
 
    }); 
 
    $('.select2').click(function() { 
 
    if ($(this).text() == "New Goods"){ 
 
     $("#category").remove(); 
 
     $(this).append("<input id='category' type='hidden' name='cat' value='New Goods' />"); 
 
    } 
 
    if ($(this).text() == "Used Goods"){ 
 
     $("#category").remove(); 
 
     $(this).append("<input id='category' type='hidden' name='cat' value='Used Goods' />"); 
 
    } 
 
    }); // end of click function 
 
}); // end of document ready 
 
     
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 
<form action="post.php" method="POST"> 
 
    <p class="select1">Northern Cape</p> 
 
    <p class="select1">Eastern Cape</p> 
 
    <p class="select2" >New Goods</p> 
 
    <p class="select2" >Used Goods</p> 
 
    <input id="submt" type="submit" name="submit" value="Submit"> 
 
</form> 
 
</html>

試試這個代碼,希望它會幫助你。

相關問題