0

in my code i am creating a drop down box and storing the changed value of the drop down in hidden variable.

<!DOCTYPE html>
<html>
<head>
  <style>

  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <select id="sweets">
    <option>Chocolate</option>
    <option selected="selected">Candy</option>

    <option>Taffy</option>
    <option selected="selected">Caramel</option>
    <option>Fudge</option>
    <option>Cookie</option>

  </select>
  <input type="hidden" id="inputfield">
  <div></div>
<script type="text/javascript">
$(document).ready(function() {
  $("#sweets").change(function() {
    var var_name = $(this).val();
    $('input[id=inputfield]').val(theValue);
      }
    });
  });
});
</script>
</body>
</html>


<?php

if(isset($_POST['form']))
echo $_POST['inputfield'];
?>

once the combo box is changed the php should get the hidden field value and i ve to perform db transaction. again i need to load another drop down box based on the selected value.. Can you guide me

4

3 回答 3

1

你可以通过隐藏字段绕过所有的马交易,直接通过ajax发送给php。

$(document).ready(function() {
  $("#sweets").change(function() {
    $.post('myphpfile.php', {sweet : $(this).val() });
  });
});

myphpfile.php 将接收值作为名称为“甜蜜”的帖子

于 2012-09-12T02:20:23.677 回答
0

除非我有误解,否则不应该

$('input[id=inputfield]').val(theValue);

$('input[id=inputfield]').val(var_name);

?

于 2012-09-12T02:17:41.320 回答
0

您可以将 select 和 hidden 元素包装在 form 元素中,添加 post 方法和 action 属性。为隐藏字段命名,如 name="hidden field" 这样您就可以在 post 变量中访问它。

<form action="currentpage.php" method="post">
 <select id="sweets">
    <option>Chocolate</option>
    <option selected="selected">Candy</option>

    <option>Taffy</option>
    <option selected="selected">Caramel</option>
    <option>Fudge</option>
    <option>Cookie</option>

  </select>
  <input type="hidden" id="inputfield" name="hiddenfield"/>
  <input type="submit" value="Submit" name="submit"/>
</form>

当执行更改事件以更新隐藏字段并在 php 中提交时,您可以执行

if(isset($_POST["submit"]))
{
   $val = $_POST["hiddenfield"] ;
}
于 2012-09-12T02:19:09.290 回答