Reputation: 167
I'm trying to get content from JavaScript to php. Basically the List1 and List2 of the selected value from the dropdown menu I want to get in $category and $subcategory. I don't know javascript at all so this is probably a very basic thing I'm trying to do.
<script type="text/javascript">
var categories = [];categories["startList"] = ["Apparel","Books"]
categories["Apparel"] = ["Men","Women"];
categories["Books"] = ["Biography","Fiction","Nonfiction"];
categories["Men"] = ["Shirts","Ties","Belts"];
categories["Women"] = ["Blouses","Skirts","Scarves"];
categories["Biography"] = ["Contemporay","Historical","Other"];
categories["Fiction"] = ["Science","Romance"];
var nLists = 2; // number of lists in the set
function fillSelect(currCat,currList){
var step = Number(currList.name.replace(/\D/g,""));
for (i=step; i<nLists+1; i++) {
document.forms[0]['List'+i].length = 1;
document.forms[0]['List'+i].selectedIndex = 0;
}
var nCat = categories[currCat];
for (each in nCat) {
var nOption = document.createElement('option');
var nData = document.createTextNode(nCat[each]);
nOption.setAttribute('value',nCat[each]);
nOption.appendChild(nData);
currList.appendChild(nOption);
}
}
function getValue(L2, L1) {
alert("Your selection was:- \n" + L1 + "\n" + L2 );
}
function init() {
fillSelect('startList',document.forms[0]['List1'])
}
navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);
</script>
</head>
<body>
<form action="">
<select name='List1' onchange="fillSelect(this.value,this.form['List2'])">
<option selected>Make a selection</option>
</select>
<select name='List2' onchange="getValue(this.value,this.form['List1'].value)">
<option selected >Make a selection</option>
</select>
</form>
</body>
</html>
.
.
input type="Submit" name="Send" value="Search">
</form>
</body>
</html>
<?php
$response = array();
require_once 'Test21/funciones_bd.php';
$db = new funciones_BD();
$category = $_POST["List1"];
$subcategory = $_POST["List2"];
.
.
.
?>
Upvotes: 0
Views: 101
Reputation: 309
get this function to post instead of alert. You need to include jquery to be able to use $.post
function getValue(L2, L1) {
alert("Your selection was:- \n" + L1 + "\n" + L2 );
}
function getValue(L2, L1) {
$.post( "urltopage.php", { List1: L1, List2: L2 } );
}
Upvotes: 1
Reputation: 866
You would have to submit the form to the same page. This can be done using the standard form submit, or via AJAX. JavaScript is a client-side language, while PHP is server-side. What that means is that data can be manipulated in JavaScript without sending a request to the server, but a server request is necessary to access data with PHP.
Using AJAX would avoid have a page refresh and would probably provide a little bit better user experience. For more information on AJAX, visit here or just do a Google search for "Javascript AJAX".
Upvotes: 1