Will_S
Will_S

Reputation: 71

Get selected value ajax

First Select

<Select id="drop1" onchange="drop1(this.value)">
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>

<div id = "txtHint1"></div>

Once a value has been selected, the value will be sent to ajax. And corresponding dropdown will be generated on another page and show in div txthint1.

Second Select

<Select id="drop2" onchange="drop2(this.value)">
<option value ='111'>111</option
<option value ='222'>222</option
<option value ='333'>333</option
</select>

<div id = "txtHint2"></div>

As the generated dropdown is another php page, how can I get what the value the user has selected to show on my current page (div txthint2)?

I will be working in php.

$value = $_GET['a'];

There are no submitted button.

Upvotes: 0

Views: 2024

Answers (1)

Dan Kanze
Dan Kanze

Reputation: 18595

Send out the second request on a successful response:

function one() {
    $.ajax({
        ...
        success: function() {
            two(TheValueFromOne);
        }
    });
}

function two() {
    $.ajax({
        ...
    });
}

What is happening is that once the request has been made successfully, then it is automatically starting the next request right after.

However, if you are jsut passing values around form one page to another you really do not need AJAX.

FOR EXAMPLE:

test1.php:

<form action="test2.php" method="get">
Book Title <br />
<input type="text" name="booktitle" /><br />
<input type="submit" name="submit" value=" - Update Database - " />
</form> 

test2.php

<body>
<? echo $_GET["booktitle"]; ?>
</body>

IN YOUR CASE:

test1.php:

<Select id="drop1" name="test1">
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>

test2.php

<Select id="drop1">
<option selected = "<? echo $_POST['test1']; ?>"></option>
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>

@QUESTION:

<div id = "txtHint2"><? echo $_POST['test1']; ?></div>

@POSTING WITH FORM:

<form method="post" action="test2.php" >

<Select id="drop1" name="test1" onChange="this.parentNode.submit()">
<option value ='1'>1</option
<option value ='2'>2</option
<option value ='3'>3</option
</select>

</form>

Upvotes: 2

Related Questions