如何将 id 从 select 传递到 PHP
How to pass the id from select to PHP?
<script>
function firstStep(element) {
var select_name = element.name;
var option_value = element.value;
var option_user_selection = element.options[ element.selectedIndex ].value;
console.log(option_user_selection);
element.id=option_user_selection;
}</script>
</head>
<body>
<div class="bck">
<form name="login" action="" method="post" accept-charset="utf-8">
<label for="usermail">Username</label>
<input type="text" name="nume" placeholder="username" required>
<label for="password">Password</label>
<input type="password" name="password" placeholder="password" required>
<input type="submit" value="Login">
</form>
<?php
$l=0;
$nl=1;
$connection = mysqli_connect("127.0.0.1", "root", "", "agentie");
if(isset($_POST['nume']) && isset($_POST['password'])) {
echo $_POST["nume"];
echo $_POST["password"];
$nl=0;
$query1 = mysqli_query($connection, "SELECT user, password FROM user u WHERE u.user='$_POST[nume]'") or die("Error in the consult.." . mysqli_error($connection));
while ($row = mysqli_fetch_assoc($query1)) {
if (($row["user"] == $_POST["nume"]) && ($row["password"] == $_POST["password"]))
{
echo "Bine ati venit" . $row["user"];
$l=1;
?>
<span>Alegeti actiunea:</span>
<select class="nume" name="optiune" onchange="firstStep(this)" id="select">
<option value="select">Afiseaza</option>
<option value="insert">Introdu</option>
<option value="delete">Sterge</option>
<option value="update">Modifica</option>
</select>
<script>
$(document).ready(function() {
$('.nume').on('change',function(){
var id = $(this).attr('id');
alert(id);
});
});
</script>
<?php
}
}
if($l==0) {
?>
<div style="position:relative; top:300px;" class="col-md-offset-3 col-md-8">
<span class="titlu">Jermaine Modelling</span>
</div>
<?php
}
}
if(($l==0) && ($nl==1)) {
?>
<div style="position:relative; top:300px;" class="col-md-offset-3 col-md-8">
<span class="titlu">Jermaine Modelling</span>
</div>
<?php
}
?>
</div>
</body>
根据所选的选项,我希望对PHP和HTML做一些事情;所以我用Javascript更改了onChange事件选择的id,但我不知道如何将更改后的id传递给PHP;我该怎么做(我知道还有另一个类似的问题被问到, 但我不知道这与该功能有关)?或者,当选择一个选项时,有没有表单(因为我已经有一个表单)时,有没有另一种方法可以使用 PHP 制作一些东西?
如果要将任何值动态传递给表单,则需要使用脚本中已有的函数。(在函数()中查找注释);
<script>
$(document).ready(function() {
$('.nume').on('change',function(){
var id = $(this).attr('id');
alert(id); // You can delete that once you tested and it works.
//we need send this data, so we will need to
//create or update hidden input with value that we got
$('form[name="login"]').append('
<input type="hidden" name="id" value="'+id+'">
这是您的"-> id <-" - 以 PHP $_POST['id'] 访问它
');
});
});
</script>
相关文章:
- 如何在PHP中使用$_POST获取Select元素值
- php使用ajax创建了select标记
- 如何使用php以json中的数组形式返回mysql-select
- 获取 PHP 中 HTML 标记 'select' 的值
- 使用PHP和AJAX更新select下拉选项
- PHP/Javascript Select listner
- .post javascript with PHP 以启用 select 语句返回
- 如何将 id 从 select 传递到 PHP
- 通过 Javascript/Jquery/AJAX 将 Select Value 传递给 php varibale
- Php select onchange dons'似乎不起作用
- Select上通过Ajax将Javascript变量传递到PHP文件中的问题
- 在php中请求一个select选项
- 使用php的动态变量从sql select和update语句的下拉列表中获取
- 使用JavaScript从select中获取值,以便在PHP中使用
- 使用Angular.js和PHP在select下拉列表中获取问题
- Ajax和PHP:SELECT查询数据库
- 如何将select选项传递到弹出对话框-php codeginiter
- php、html,在写入第一个单词后将select更改为selective,依此类推
- 使用jQuery/Ajax过滤PHP SELECT查询与下拉框
- 我可以循环php SELECT名称与javascript