Jump to content

Using JavaScript with PHP


harryuk

Recommended Posts

Hi,

I would like to use the $_POST function for the sub-category section of the form. The problem i am having is how i can do this as i am using javascript to automatically update the sub-category section when a item is selected in the category section. So where and how would i put the $_POST function so i can echo the form info including what sub-category item i selected out on my index.php page.

 

I hope that makes sense. Please help

Thank you

team = new Array(
new Array(
new Array("Toesavers", 39482304),	
new Array("Dr. Martens Industrial", 34802389),
new Array("Himalayan", 39823498),
new Array("Caterpillar", 87587343),
new Array("Timberland", 68798735)
),
new Array(
new Array("Hand Protection", 23840238),
new Array("Eye & Face Protection", 92390484),
new Array("WeatherWear", 29048203),
new Array("Head Protection", 94098230),
new Array("Hearing Protection", 39234923),
new Array("Respiratory Protection", 29345423)
),
);
function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;
// empty existing items
for (i = selectCtrl.options.length; i >= 0; i--) {
selectCtrl.options[i] = null;
}
prompt = (itemArray != null) ? goodPrompt : badPrompt;
if (prompt == null) {
j = 0;
}
else {
selectCtrl.options[0] = new Option(prompt);
j = 1;
}
if (itemArray != null) {
// add new items
for (i = 0; i < itemArray.length; i++) {
selectCtrl.options[j] = new Option(itemArray[i][0]);
if (itemArray[i][1] != null) {
selectCtrl.options[j].value = itemArray[i][1];
}
j++;
}
selectCtrl.options[0].selected = true;
   }
}
<form>
<select name="Make" onchange="fillSelectFromArray(this.form.Team, ((this.selectedIndex == -1) ? null : team[this.selectedIndex-1]));">
<option value="-1" selected="selected">Select Category</option>
<option value="1">Footwear</option>
<option value="2">PPE</option>
<option value="3">Ladders</option>
</select></label>
  <label>
    Sub-Category
  <select name="Team">
  <option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select></label>
</form>

Link to comment
https://forums.phpfreaks.com/topic/243505-using-javascript-with-php/
Share on other sites

Well I don't really get what's supposed to happen but I understand you have some catalog or something where users select stuff and when they select something some other stuff is autoupdated and you want this autoupdated stuff to be accessible in another page via post, is that correct?

Yes exactly. Under the sub-category section all the option tags have no values until an item is selected on the category section. Usually you would have values already in the option tag so you can add your $_POST function in order to send all form info to another page. But in this instance i do not have any values to be able to add a $_POST function?!?!

Sure

 

when you're setting up the page if you have some passed data from another page which you need to access in javascript you can do this

 

<? echo "<input type='hidden' id='someid' value='Received data here'/>"; ?>

 

then you access it in js like so

 

thedata = document.getElementById('someid').value

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.