Ingila Ejaz
Ingila Ejaz

Reputation: 233

Select dropdown value not posted PHP

I am making a new mobile form that is used as a pop-up on button click.

I have this PHP and Javascript where I'm posting values to another page via 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;
?>

The problem is I only get $model value but not that of $make. Can anyone help?

Upvotes: 1

Views: 695

Answers (3)

Nishit Maheta
Nishit Maheta

Reputation: 6031

change code as below .

in ajax call you pass comboA value with 'make='+make+'. so you will not get comboA without passing as data.

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;
?> 

Option 2

or change your data string as below . replace make with comboA

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

PHP

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

as yo are using jquery library you can change your code as below to get value.

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

Upvotes: 4

Manish Shukla
Manish Shukla

Reputation: 1365

Try this code

var e = document.getElementById("comboA");

var make = e.options[e.selectedIndex].value;

if you are usig jQuery

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

Upvotes: 1

Rex Rex
Rex Rex

Reputation: 1030

Use like this

$make = $_POST['make'];

Upvotes: 1

Related Questions