0

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>

&nbsp;
<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"];
.
.
.
?>
4
  • 1
    Serialize the information using jQuery, then send it via an AJAX call to the server. Commented Mar 25, 2014 at 15:40
  • Implying I know jQuery and AJAX. Commented Mar 25, 2014 at 15:41
  • 1
    have a look here: api.jquery.com/jQuery.post Commented Mar 25, 2014 at 15:42
  • If I didn't need the cascading dropdown menus I would have just used php and html to send to the server. I tried it and it works but since I wanted cascading menus I had to do with javascript. Commented Mar 25, 2014 at 15:43

2 Answers 2

1

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".

Sign up to request clarification or add additional context in comments.

Comments

1

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 } );
}

2 Comments

If you look at the documentation, you can then get a bit more clever with .success, .fail, etc
To include jquery, link to the latest one on google or download it into your own application and just add the script tags on your html page that contains the form <script type="text/javascript" src="/js/jquery-latest.min.js"></script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.