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
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
Share on other sites

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.