/    Sign up×
Community /Pin to ProfileBookmark

Get Selected Option Value and Insert To PHP Variable – With/Without refreshing.

Hi,

I am trying to get the selected option value and insert into a php variable, without refreshing the page or submitting a button, but only by the on-change event.

Here is what i have done so far after some research though struggling to get it to work.

[B]HTML[/B]

[code=html]
<select id=”dropdown1″ class=”select” tabindex=”2″ onchange=”run(this)”>
<option>Select</option>
<option value=”volvo”>Volvo</option>
<option value=”saab”>Saab</option>
<option value=”mercedes”>Mercedes</option>
<option value=”audi”>Audi</option>
</select>
[/code]

[B]Jquery/Ajax[/B]

[CODE]
function run(sel) {

var i = sel.selectedIndex;
if (i != -1) {
//document.getElementById(“car”).value = sel.options[i].text;

$.ajax({
type: “POST”,
url: “home.php”,
data: { car: sel.options[i].text}

}).done(function( msg ) {});
}

}
[/CODE]

[B]PHP[/B]

[code=php]
if(isset($_POST[“car”])){
$_SESSION[‘car’] = $_POST[“car”];
$myCar = $_SESSION[‘car’] ;
echo $myCar;

}
[/code]

Again just to clarify the user will select an option from the drop down it will be stored in a php variable to use later down the page for something else. This needs to be done without page refreshing (if possible). If possible provide solution with and without page refresh.

Any help is appreciated,

regards.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionJul 30.2014 — I *think* you'll need to create a PHP function that will store a session variable. So you'll use JavaScript to listen for the change, then using AJAX, you'd send the information to the PHP function to create the session variable. I'm not sure if that's the best way or not, but it should work!
Copy linkTweet thisAlerts:
@ginerjmJul 30.2014 — Very confusing post.

1 - You apparently want to use JS to grab a just-changed dropdown.

2 - Then you want to set a php variable. That's kind of hard to do since you have no immediate access to php from js

3 - You then show us some ajax code that is going to pass your new option to a php script.

4 - Your script then returns(?) a value for something on your current web page? Your JS can then update that html value with the returned value.

Now that I've laid out your apparent activities your post makes sense, although the nomenclature is still incorrect.

Is this a correct visualization of your intent?
Copy linkTweet thisAlerts:
@M-S-MAug 07.2019 — <!-- page name (addinfo.php) -->

<select name="number" id="number" onchange="pass_value(this.value)">

<option value="1">one</option>

<option value="2">two</option>

<option value="3">three</option>

</select>

<script>

// using ajax

function pass_value(value){

var xhttp;


if (value == "") {

return;

}

xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {


}
};

xhttp.open("GET", "addinfo.php?set_value="+value, true);

xhttp.send();

}

</script>

<?php

// this code in (addinfo.php) page

if(isset($_GET['set_value'])){

session_start();

$_
SESSION['set_value']=$_REQUEST['set_value'];

}

?>
Copy linkTweet thisAlerts:
@NogDogAug 07.2019 — Locking thread...

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-08-07/1565187744-167639-screen-shot-2019-08-07-at-102115-am.png]
Copy linkTweet thisAlerts:
@NogDogAug 07.2019 — {"locked":true}
×

Success!

Help @KKHAN spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...