Jump to content

AJAX, PHP Help


LiamKeenan

Recommended Posts

HTML FORM DATA

<form id="" method="POST" onsubmit="return false;">
			Name: <input type="radio" name="DoorFrame" id="DoorFrame" value="Door 1" onclick="sendRequest()" />
            Name: <input type="radio" name="DoorFrame" id="DoorFrame" value="Door 2" onclick="sendRequest()" />
			<input type="submit" value="submit" />
		</form>
		
		<div id="show"></div>

AJAX

<script type="text/javascript" src="prototype.js"></script>
		<script>

			function sendRequest() {
                var DoorFrame = $('DoorFrame').value;
				new Ajax.Request("test.php", 
					{ 
					method: 'POST', 
					postBody: $("DoorFrame").serialize(),
					onComplete: showResponse 
					});
				}

			function showResponse(req){
				$('show').innerHTML= req.responseText;
			}
		</script>

PHP

if (isset($_POST['DoorFrame'])) {
    echo "image here" . isset($_POST['DoorFrame']) ? "image here" . $_POST['DoorFrame'] : '';
}
else {
   echo "EMPTY"; 
}

The script is currently getting one value from the form but what I want to do is get both values depending on which one is chosen obviously but I also want to add another group of radio buttons and get the value selected from them as well.

 

I am new to ajax but pretty good with php.

 

Kind Regards,

 

 

Link to comment
https://forums.phpfreaks.com/topic/275498-ajax-php-help/
Share on other sites

First, you shouldn't have the same ID on the two radio buttons, so change them to DoorFrame1 and DoorFrame2 or whatever.

 

Then also send a parameter to the sendRequest() function:

Name: <input type="radio" name="DoorFrame" id="DoorFrame1" value="Door 1" onclick="sendRequest(1)" />
Name: <input type="radio" name="DoorFrame" id="DoorFrame2" value="Door 2" onclick="sendRequest(2)" />

 

In the sendRequest() function, fetch the parameter and send this in the AJAX request instead, then in PHP you can see if it's 1 or 2, and use that to determine what to do.

function sendRequest(door) {
    var DoorFrame = $('DoorFrame').value;
    new Ajax.Request("test.php", 
    { 
        method: 'POST', 
        postBody: door.serialize(),
        onComplete: showResponse 
    });
}

I thinnnk that should work.

 

- W

Link to comment
https://forums.phpfreaks.com/topic/275498-ajax-php-help/#findComment-1418197
Share on other sites

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.