Jump to content

Recommended Posts

Does anybody have a chunk of code i could use?

 

Im struggling finding something to pass 2 values from 2 select boxes to a php form and return a result. Sounds so easy but i cant make it work and im a noob at javascript/ajax.

 

I have got it to send one of the 2 values but the other value that gets sent is undefined.

 

Thanks,

Justin

Link to comment
https://forums.phpfreaks.com/topic/251137-passing-multiple-values-with-ajax/
Share on other sites

AJAX

<script>
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}

	return xmlhttp;
    }



function getfunit(modelId,locationId) {		
	var strURL="getfunit.php?model="+document.fparts.model.vaule+"&location="+document.fparts.location.value;
	var req = getXMLHTTP();

	if (req) {

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('funitdiv').innerHTML=req.responseText;						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("POST", strURL, true);
		req.send(null);
	}

}
</script>

 

 

form code

                                    <form name="fparts" action="<?=$_SERVER['PHP_SELF']?>" method="post">
                                    <label></label>
                                        <select name="model">
                                            <option value="default" selected="selected">Please Select Machine</option>
                                            <option value="HB-P830">HB-P830</option>
                                            <option value="HB-830">HB-830</option>
                                            <option value="HB1030S1">HB-1030 Series 1</option>
                                            <option value="HB1030S2">HB-1030 Series 2</option>                                            
                                            <option value="HB1430S1">HB-1430 Series 1</option>
                                            <option value="HB1430S2">HB-1430 Series 2</option>
                                            <option value=""></option>
                                            <option value="HB-P830CE">HB-P830CE</option>
                                            <option value="HB-830CE">HB-830CE</option>
                                            <option value="HB1030CES1">HB-1030CE Series 1</option>
                                            <option value="HB1030CES2">HB-1030CE Series 2</option>                                            
                                            <option value="HB1430CES1">HB-1430CE Series 1</option>
                                            <option value="HB1430CES2">HB-1430CE Series 2</option>
                                        </select>
                                        <label></label>
                                        <select name="location" onChange="getfunit()">
                                            <option value="default" selected="selected">Please Select Part Location</option>
                                            <option value="Base">Base</option>
                                            <option value="Decals">Decals</option>
                                            <option value="Lower Control">Lower Control</option>
                                            <option value="Misc">Misc</option>
                                            <option value="Railing">Railings</option>
                                            <option value="Scissors">Scissors</option>
                                            <option value="Upper Control">Upper Control</option>
                                        </select>                                     
                                        <label></label>   
                                        <div id="funitdiv"><select name="funit">
                                            <option>Please Select Functional Circuit</option>
                                                </select></div>
                                        <label></label>                                                                                 
                                        <input type="submit" name="submit" value="Submit" id="submit">
                                    </form>

 

 

php processing code

<? $modelId=$_POST['model'];
$locationId=$_POST['location'];
$link = mysql_connect('localhost', 'custkis6_dealers', 'locate'); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('custkis6_parts');
$query="SELECT fcircuit FROM hybrid WHERE model='$modelId' AND plocation='$locationId'";
$result=mysql_query($query);

?>
<select name="funit">
<option>Select Functional Circuit</option>
<option><? echo $modelId ?></option>
<option><? echo $locationId ?></option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['fcircuit']?></option>
<? } ?>
</select>

 

This is a sample request and im getting undefined under model yet but the location is coming out right.

Request URL:http://hybridlifts.com/position/pages/ss/getfunit.php?model=undefined&location=Base

Request Method:POST

Status Code:200 OK

POST changed to get and i got my values. YAY... now to strip the results so i dont have excessive repeating. Think i can pull this one off unless anybody has a quick fix there too.

 

http://hybridlifts.com/position/pages/ss/parts2.php

 

I got some tweaking to do now... seems the functional unit get populated but when i run my php post action im not passing a value .... i think i might have it though

 

 

THANKS SO much for pointing out my typo!!!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.