gpt4 book ai didi

javascript - 选择未发布 PHP 的下拉值

转载 作者:行者123 更新时间:2023-11-30 08:00:26 25 4
gpt4 key购买 nike

我正在制作一个新的移动表单,用作点击按钮时的弹出窗口。

我有这个 PHP 和 Javascript,我通过 JQuery 将值发布到另一个页面:

<form action = "#" method = "POST" id = "newFrm" name = "newFrm" >
Please select Make:
<Select name = "comboA" id="comboA">
<option value = "1">Samsung</option>
<option value = "2">iPhone</option>
<option value = "3">Sony Ericsson</option>
</Select>
Please specify Model and version:
<input type = "text" required id = "model" name = "model"></input>
<input type = "submit" value = "Enter" id = "enter" name = "enter"> </input>

<script>
$( "#enter" ).click(function() {

var model= document.getElementById('model').value;
var make= document.getElementById('comboA').value;
var dataString = 'make='+make+'&model='+model;
if($.trim(make).length>0 && $.trim(model).length>0)
{

$.ajax({
type: "POST",
url: "newMobile.php",
data: dataString,
cache: false,
beforeSend: function(){ $("#enter").val('Connecting...');},
success: function(data){

if(data == 1)
{
alert("Information stored successfully!");

}

else{}
if (data == 0)
{
alert("Some error occured! Please try again.");
}

else{}
}
});
return false;
});
</script>
</form>

newMobile.PHP

<?php
$model = $_POST['model'];
$make = $_POST['comboA'];
echo $make.$model;
?>

问题是我只获得了$model 的值,而不是$make 的值。谁能帮忙?

最佳答案

如下更改代码。

在 ajax 调用中,您使用 'make='+make+' 传递 comboA 值。所以你不会得到 comboA 不作为数据传递。

var obj = document.getElementById("comboA");
var make= obj.options[obj.selectedIndex].value;

var dataString = 'make='+make+'&model='+model;

PHP

<?php
$model = $_POST['model'];
$make = $_POST['make']; // use make not comboA
echo $make.$model;
?>

选项 2

或如下更改您的数据字符串。将 make 替换为 comboA

var dataString = 'comboA='+make+'&model='+model;

PHP

<?php
$model = $_POST['model'];
$make = $_POST['comboA'];
echo $make.$model;
?>

因为您正在使用 jquery 库,您可以如下更改代码以获得值(value)。

var model= $('#model').val();
var make= $('#comboA').val();

关于javascript - 选择未发布 PHP 的下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29621017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com