• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4038
  • Last Modified:

Ajax radio button value with php

I have these radio buttons but it doesn't send the value to data base for some reason. can someone help
Thanks

<div style="width:100%;  border:solid 1px; margin-left:2%; height:105px;">
 
  <div style="width:25%;   float:left;">
   
  </div>
 
  <div style="width:74%;  float:right;">
 
    <div style="width:64%; border:solid 1px; float:right; background:#FFFFCC;">
     
1<input name="PC" type="radio"  id ="PC1" value="1" <?php if ($row["PC"]==1) echo "checked"; ?> onChange="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
2<input name="PC" type="radio" id ="PC2" value="2" <?php if ($row["PC"]==2) echo "checked"; ?> onChange="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
3<input name="PC" type="radio" id ="PC3" value="3" <?php if ($row["PC"]==3) echo "checked"; ?> onChange="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
4<input name="PC" type="radio" id ="PC4" value="4"  <?php if ($row["PC"]==4) echo "checked"; ?> onChange="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
5 <input name="PC" type="radio" id ="PC5" value="5" <?php if ($row["PC"]==5) echo "checked"; ?> onChange="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
6<input name="PC" type="radio" id ="PC6" value="6" <?php if ($row["PC"]==6) echo "checked"; ?> onChange="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
7 <input name="PC" id ="PC7" type="radio" value="7" <?php if ($row["PC"]==7) echo "checked"; ?> onChange="ajaxFunction('PC');" /><div id="updatePC"></div>
    </div>
 
    <div style="width: 35%; float: right;">
   
      <!-- 69, because 100-30=70, but you use 1px border, so it's more than 100% -->
     



    </div>
 
  </div>
 
</div>


Below is my ajax code
function ajaxFunction(element){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Please update your browser!");
				return false;
			}
		}
	}
	var url="mainsurvey.php?T=" + encodeURIComponent(document.getElementById('T').value) 
	+ "&PC1="+(document.getElementById('PC1').checked ? 1 : 0)
	+ "&PC2="+(document.getElementById('PC2').checked ? 2 : 0)
	+ "&PC3="+(document.getElementById('PC3').checked ? 3 : 0)
	+ "&PC4="+(document.getElementById('PC4').checked ? 4 : 0)
	+ "&PC5="+(document.getElementById('PC5').checked ? 5 : 0)
	+ "&PC6="+(document.getElementById('PC6').checked ? 6 : 0)
	+ '&element=' + element;
	ajaxRequest.open("GET", url, true);
    ajaxRequest.onreadystatechange = function(){            
      switch(ajaxRequest.readyState) {
        case 4:
        if(ajaxRequest.status!=200) {
            alert("Error:"+ajaxRequest.status); 
        }else{
        	var xml = ajaxRequest.responseXML;
            var updateElement = xml.getElementsByTagName("element")[0];
            document.getElementById('update' + updateElement.firstChild.nodeValue).innerHTML = '<img src="img/greencheck.jpg" height="20" width="20" alt="Updated">';
        }
        break;
 
        default:
            return false;
        break;     
    }
  };	
	ajaxRequest.send(null);
 
 
 
}
function changeColor(obj,color){
obj.style.backgroundColor = color;
}

Open in new window

0
syedasimmeesaq
Asked:
syedasimmeesaq
  • 37
  • 19
  • 2
1 Solution
 
hieloCommented:
use onclick:
onclick="ajaxFunction('PC');"

NOT onchange:
onChange="ajaxFunction('PC');"
0
 
jarospiritCommented:
I think I solve something similar by pre-selecting only one item, and hidding it with css(so it was not display in the browser), then allowing the user to make the selection.

it helped me a lot, to look at the render HTML code in every step of the process.

It seem without seeing the rest of the code that the error is on the form code and the way you are capturing the result from the user.

hope that helps.


0
 
syedasimmeesaqAuthor Commented:
I changed it to onclick and it still doesn't send value to db.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
syedasimmeesaqAuthor Commented:
there is something wrong with this part
+ "&PC1="+(document.getElementById('PC1').checked ? 1 : 0)
      + "&PC2="+(document.getElementById('PC2').checked ? 2 : 0)
      + "&PC3="+(document.getElementById('PC3').checked ? 3 : 0)
      + "&PC4="+(document.getElementById('PC4').checked ? 4 : 0)
      + "&PC5="+(document.getElementById('PC5').checked ? 5 : 0)
      + "&PC6="+(document.getElementById('PC6').checked ? 6 : 0)

because if I use
+ "&PC="+(document.getElementById('PC1').checked ? 1 : 0) (notice I used "&PC instead of "&PC1 )
it collects the first radio button data only
thanks
0
 
syedasimmeesaqAuthor Commented:
I am not sure how in javascript it works but I think we need something like this there
if (PC ==1) then id=PC1
if(PC==2) then id = PC2...something like that
0
 
jarospiritCommented:
Can you include the PHP code that is making the selection load to the DB.

if you can simplified it so you only send the  to just that ready button.

something like this:

<?php
 
	echo 'The value of PH: '.$_POST['PC']; 
 
 
?>
<form action="" method="post">
 
	1<input name="PC" type="radio" value="1">
    2<input name="PC" type="radio" value="2">
    3<input name="PC" type="radio" value="3" checked>    
	<input name="Send" type="submit" id="Send" value="Submit">
    
</form>

Open in new window

0
 
hieloCommented:
Everything else look fine. The only thing "questionable" is the fact that I see this:

<div id="updatePC"></div>
 
but on your ajax function I see:
document.getElementById('update' + updateElement.firstChild.nodeValue).innerHTML = '<img src="img/greencheck.jpg" height="20" width="20" alt="Updated">';
 
My expectation would have been something along the lines of:
document.getElementById('update' + element).innerHTML = updateElement.firstChild.nodeValue + '<img src="img/greencheck.jpg" height="20" width="20" alt="Updated">';

Open in new window

0
 
syedasimmeesaqAuthor Commented:
jarospirit: php script is working fine. I am 100% sure that problem is where I indicated above.
Thanks
0
 
syedasimmeesaqAuthor Commented:
hielo: could you look into the code above . I am pretty sure that something is wrong there. I tested it with just
+ "&PC="+(document.getElementById('PC1').checked ? 1 : 0)
and not
+ "&PC1="+(document.getElementById('PC1').checked ? 1 : 0)
and it sends the data to database if the value is 1. Problem is how would I send other radio buttons value too. But it tells me that something is wrong there.
Thanks

0
 
syedasimmeesaqAuthor Commented:
any idea on this?
Thanks guys
0
 
hieloCommented:
what you posted works ok. I add an element with id="T", otherwise this:
encodeURIComponent(document.getElementById('T').value)

would give you a runtime error. Save what I have as test.html. Notice the alert(url) statement. What does YOUR copy alert?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
<head>
<title></title>
<script type="text/javascript">
function ajaxFunction(element){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Please update your browser!");
				return false;
			}
		}
	}
	var url="mainsurvey.php?T=" + encodeURIComponent(document.getElementById('T').value) 
	+ "&PC1="+(document.getElementById('PC1').checked ? 1 : 0)
	+ "&PC2="+(document.getElementById('PC2').checked ? 2 : 0)
	+ "&PC3="+(document.getElementById('PC3').checked ? 3 : 0)
	+ "&PC4="+(document.getElementById('PC4').checked ? 4 : 0)
	+ "&PC5="+(document.getElementById('PC5').checked ? 5 : 0)
	+ "&PC6="+(document.getElementById('PC6').checked ? 6 : 0)
	+ '&element=' + element;
	ajaxRequest.open("GET", url, true);
	alert(url);return false;
    ajaxRequest.onreadystatechange = function(){            
      switch(ajaxRequest.readyState) {
        case 4:
        if(ajaxRequest.status!=200) {
            alert("Error:"+ajaxRequest.status); 
        }else{
        	var xml = ajaxRequest.responseXML;
            var updateElement = xml.getElementsByTagName("element")[0];
            document.getElementById('update' + updateElement.firstChild.nodeValue).innerHTML = '<img src="img/greencheck.jpg" height="20" width="20" alt="Updated">';
        }
        break;
 
        default:
            return false;
        break;     
    }
  };	
	ajaxRequest.send(null);
 
 
 
}
function changeColor(obj,color){
obj.style.backgroundColor = color;
}
 
</script>
</head>
<body>
<div style="width:100%;  border:solid 1px; margin-left:2%; height:105px;">
 
  <div style="width:25%;   float:left;">
   
  </div>
 
  <div style="width:74%;  float:right;">
    <input id="T" value="test"/>   
    <div style="width:64%; border:solid 1px; float:right; background:#FFFFCC;">
 
1<input name="PC" type="radio"  id ="PC1" value="1"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
2<input name="PC" type="radio" id ="PC2" value="2" onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
3<input name="PC" type="radio" id ="PC3" value="3"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
4<input name="PC" type="radio" id ="PC4" value="4"   onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
5 <input name="PC" type="radio" id ="PC5" value="5"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
6<input name="PC" type="radio" id ="PC6" value="6"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
7 <input name="PC" id ="PC7" type="radio" value="7"  onclick="ajaxFunction('PC');" /><div id="updatePC"></div>
    </div>
 
    <div style="width: 35%; float: right;">
    
      <!-- 69, because 100-30=70, but you use 1px border, so it's more than 100% -->
     
 
 
 
    </div>
 
  </div>
 
</div>
 
</body>
</html>

Open in new window

0
 
syedasimmeesaqAuthor Commented:
my copy doesn't alert anything. It just doesn't send the value of the radio buttons to the database. Everything else works fine. If you would like I can post the complete code ..
Thanks
0
 
hieloCommented:
>>my copy doesn't alert anything
That tells me that there's some javascript runtime error somewhere that is "halting" the javascript engine. Does Firebug report any errors?

>>If you would like I can post the complete code
Ok
0
 
hieloCommented:
post the code generated by your php script, not the actual php code.
0
 
syedasimmeesaqAuthor Commented:
>>>post the code generated by your php script, not the actual php code.

sorry, what do u mean?
0
 
hieloCommented:
>>sorry, what do u mean?
IE6: View > Source
FF: View > Page Source  OR Ctrl+U
0
 
syedasimmeesaqAuthor Commented:
here is all the code.
here is the form
<?php
session_start();
require_once("connect_info.php");
if
   (
(isset($_GET['PC']))
)
 
{
require_once("connect_info.php");
$myres = "update survey set
                                    
                                     PC= '".$_GET['PC']."'
                                       where ID=1";
                                     
                                     
      $inrec = mysql_query($myres);
      header("Content-Type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    echo "<response><element>";
    echo $_GET['element'];
    echo "</element></response>";
    die();

}
 
include_once('upper.php'); ?>
 
<script type="text/javascript" src="ajaxfunctions.js"></script>
<style type="text/css">
<!--
.style1 {
        font-size: xx-small;
        font-weight: bold;
        color: #FF0000;
}
-->
</style>                        
 
<?php
require('myfunctions.php');
 require_once("connect_info.php");
 $gvalues = mysql_query("select * from survey where user = '".mysql_real_escape_string($_SESSION["user"])."'") or die ("Update error: ".mysql_error());
 $row = mysql_fetch_assoc($gvalues);
?>
<form action="" method="POST">
                       






<div style="width:100%;  border:solid 1px; margin-left:2%; height:105px;">
 
  <div style="width:25%;   float:left;">
 
  </div>
 
  <div style="width:74%;  float:right;">
 
    <div style="width:64%; border:solid 1px; float:right; background:#FFFFCC;">
     
1<input name="PC" type="radio"  id ="PC" value="1" <?php if ($row["PC"]==1) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
2<input name="PC" type="radio" id ="PC" value="2" <?php if ($row["PC"]==2) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
3<input name="PC" type="radio" id ="PC" value="3" <?php if ($row["PC"]==3) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
4<input name="PC" type="radio" id ="PC" value="4"  <?php if ($row["PC"]==4) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
5 <input name="PC" type="radio" id ="PC" value="5" <?php if ($row["PC"]==5) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
6<input name="PC" type="radio" id ="PC" value="6" <?php if ($row["PC"]==6) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
7 <input name="PC" id ="PC" type="radio" value="7" <?php if ($row["PC"]==7) echo "checked"; ?> onclick="ajaxFunction('PC');" /><div id="updatePC"></div>
    </div>
 
    <div style="width: 35%; float: right;">
   
      <!-- 69, because 100-30=70, but you use 1px border, so it's more than 100% -->
     <textarea name="T" cols="25" id ="T"rows="5" onChange="ajaxFunction('PC');"><?php echo $row["T"]; ?></textarea>



    </div>
 
  </div>
 
</div>
















 
   
   
    </form>        
           
           
           
<?php include_once('lower.php'); ?>


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

here is ajaxfunction
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

function ajaxFunction(element){
      var ajaxRequest;  // The variable that makes Ajax possible!
      try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
      } catch (e){
            // Internet Explorer Browsers
            try{
                  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                  try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e){
                        // Something went wrong
                        alert("Please update your browser!");
                        return false;
                  }
            }
      }
      var url="mainsurvey.php?T=" + encodeURIComponent(document.getElementById('T').value)
      + "&PC="+(document.getElementById('PC1').checked ? 1 : 0)
      + "&PC="+(document.getElementById('PC2').checked ? 2 : 0)
      + "&PC="+(document.getElementById('PC3').checked ? 3 : 0)
      + "&PC="+(document.getElementById('PC4').checked ? 4 : 0)
      + "&PC="+(document.getElementById('PC5').checked ? 5 : 0)
      + "&PC="+(document.getElementById('PC6').checked ? 6 : 0)
      + '&element=' + element;
      ajaxRequest.open("GET", url, true);
    ajaxRequest.onreadystatechange = function(){            
      switch(ajaxRequest.readyState) {
        case 4:
        if(ajaxRequest.status!=200) {
            alert("Error:"+ajaxRequest.status);
        }else{
              var xml = ajaxRequest.responseXML;
            var updateElement = xml.getElementsByTagName("element")[0];
            document.getElementById('update' + updateElement.firstChild.nodeValue).innerHTML = '<img src="img/greencheck.jpg" height="20" width="20" alt="Updated">';
        }
        break;
 
        default:
            return false;
        break;    
    }
  };      
      ajaxRequest.send(null);
 
 
 
}
function changeColor(obj,color){
obj.style.backgroundColor = color;
}
 


0
 
syedasimmeesaqAuthor Commented:
its exactly the samething..besides T has the value in it. The problem is that if I select the first radio button, the value goes fine and in db I can see 1. But then when I choose 2 or 3 or... it doesn't change.

Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "XHTML1-s.dtd">
<html>
<head>
<title>Project 1 </title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header">
<div id="headerleft">
<img src="img/class.jpg" alt="" width="174" height="60" />
  </div>
<div id="headerright">
<h1> P </h1>
	<h2>Z</h2></div></div>
    <div id="clearboth"> </div>
    </body>
</div>
 
</div>
 
<div id="container">
 
	<div id="primarycontainer">
		<div id="primarycontent">
 
		 
<script type="text/javascript" src="ajaxfunctions.js"></script>
<style type="text/css">
<!--
.style1 {
        font-size: xx-small;
        font-weight: bold;
        color: #FF0000;
}
-->
</style>                        
 
<form action="" method="POST">
                        
 
<div style="width:100%;  border:solid 1px; margin-left:2%; height:105px;">
 
  <div style="width:25%;   float:left;">
 
    <br /><br /><br />
  </div>
 
  <div style="width:74%;  float:right;">
 
    <div style="width:64%; border:solid 1px; float:right; background:#FFFFCC;">
      
1<input name="PC" type="radio"  id ="PC1" value="1" checked onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
2<input name="PC" type="radio" id ="PC2" value="2"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
3<input name="PC" type="radio" id ="PC3" value="3"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
4<input name="PC" type="radio" id ="PC4" value="4"   onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
5 <input name="PC" type="radio" id ="PC5" value="5"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
6<input name="PC" type="radio" id ="PC6" value="6"  onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
7 <input name="PC" id ="PC7" type="radio" value="7"  onclick="ajaxFunction('PC');" /><div id="updatePC"></div>
    </div>
 
    <div style="width: 35%; float: right;">
    
      <!-- 69, because 100-30=70, but you use 1px border, so it's more than 100% -->
     <textarea name="T" cols="25" id ="T"rows="5" onChange="ajaxFunction('PC');">This works but not radio button why?</textarea>
 
 
 
    </div>
 
  </div>
 
</div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 </div>   
    
    
    </form>        
 
 
</body>
</html>

Open in new window

0
 
syedasimmeesaqAuthor Commented:
heilo .u there?
0
 
syedasimmeesaqAuthor Commented:
in my ajax function if I do this
      + "&PC="+(document.getElementById('PC1').checked ? 1 : 0)
      + "&PC="+(document.getElementById('PC2').checked ? 2 : 0)
      + "&PC="+(document.getElementById('PC3').checked ? 3 : 0)
      + "&PC="+(document.getElementById('PC4').checked ? 4 : 0)
      + "&PC="+(document.getElementById('PC5').checked ? 5 : 0)
      + "&PC="+(document.getElementById('PC6').checked ? 6 : 0)
      + "&PC="+(document.getElementById('PC6').checked ? 7 : 0)

then it shows PC always checked to 7 even if I change it to 1 or 2 or ...6

my field name is PC but field ID is different. so I am thinking this code needs to be changed. Don't you think so?


0
 
syedasimmeesaqAuthor Commented:
Hielo sorry correction to above

in my ajax function if I do this
      + "&PC="+(document.getElementById('PC1').checked ? 1 : 0)
      + "&PC="+(document.getElementById('PC2').checked ? 2 : 0)
      + "&PC="+(document.getElementById('PC3').checked ? 3 : 0)
      + "&PC="+(document.getElementById('PC4').checked ? 4 : 0)
      + "&PC="+(document.getElementById('PC5').checked ? 5 : 0)
      + "&PC="+(document.getElementById('PC6').checked ? 6 : 0)
      + "&PC="+(document.getElementById('PC7').checked ? 7 : 0)

then it shows PC always checked to 7 even if I change it to 1 or 2 or ...6

my field name is PC but field ID is different. so I am thinking this code needs to be changed. Don't you think so?
0
 
syedasimmeesaqAuthor Commented:
@ Hielo

and if I change the field ID to PC for all and do this one line instead of all that

+ "&PC="+(document.getElementById('PC').value)

it send the value of 1 only. If I go to the database and manually change it to 2 , 3 or whichever I want then it shows that radio button checked. Other wise it will only show that the first radio button is checked. Ahhh its blowing my mind

0
 
hieloCommented:
>>heilo .u there?
I am back. Sorry. Just so we are clear, the problem is NOT about sending the correct parameters. The problem is about updating the db? Let me read all your posts more carefully.
0
 
hieloCommented:
The problem is this:
if( (isset($_GET['PC'])) )

for that to work, you need your url to have something like:
PC=something

PC=0  is NOT the same as PC1=0. Likewise:
PC=1  is NOT the same as PC1=1

What you need is to iterate over the possible elements:
0
 
hieloCommented:
most likely this is what you are after:
...
if( (isset($_GET['element'])) ) 
{
	require_once("connect_info.php");
	for($i=1; $i <= 6; ++$i)
	{
		$key="PC{$i}";
		if( !empty($_GET[$key]) )
		{
			$myres = "update survey set PC='". $_GET[$key] ."' where ID=1";
			$inrec = mysql_query($myres);
		}
	}
    header("Content-Type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    echo "<response><element>";
    echo $_GET['element'];
    echo "</element></response>";
    die();
 
}
...

Open in new window

0
 
syedasimmeesaqAuthor Commented:
so how would I fix that? I am not sure what could I do here.
Thanks
0
 
syedasimmeesaqAuthor Commented:
if I use above code, what about the other fields that are not PC? Like T
0
 
hieloCommented:
>>so how would I fix that?
Refer to my previous post.
0
 
syedasimmeesaqAuthor Commented:
ok I tried that. And it didn't make any difference
0
 
syedasimmeesaqAuthor Commented:
Hielo, is there any other way to fix it ? Thank you for your help
0
 
syedasimmeesaqAuthor Commented:
if I use the code below. Where do I put other values such as T and stuff.
PS: I tried it solo, and it didn't work
Thanks
0
 
syedasimmeesaqAuthor Commented:
if you would like I can send you complete files or attach here as text files. Would that be ok?
Thanks
0
 
syedasimmeesaqAuthor Commented:
Here are the three files. Hope this will help to find some clues. Thanks
Threefiles.zip
0
 
hieloCommented:
>>Here are the three files
Three files??? I thought that the file where you have:
 if( (isset($_GET['element'])) ) ...

is the same as mainsurvey.php. Since you are sending the ajax request to mainsurvey.php, you need to process the parameters in mainsurvey.php which is basically what you had earlier:
<?php 
session_start();
require_once("connect_info.php");
if( (isset($_GET['element'])) ) 
{
	require_once("connect_info.php");
	for($i=1; $i <= 6; ++$i)
	{
		$key="PC{$i}";
		if( !empty($_GET[$key]) )
		{
			$myres = "update survey set PC='". $_GET[$key] ."' where ID=1";
			$inrec = mysql_query($myres);
		}
	}
    header("Content-Type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    echo "<response><element>";
    echo $_GET['element'];
    echo "</element></response>";
    die();
 
}
else{
 echo "Invalid Parameter received";
}
?>
 
I'll be away for an hour or so.

Open in new window

0
 
syedasimmeesaqAuthor Commented:
hey hielo thank you for reply. Ok so again my question is that if I put the above code and lets say that it works, it will only update the PC field. I have other fields in there too. Can you please look at the zip file and may be you can tell me how to combine all this together.
Thank you again
0
 
syedasimmeesaqAuthor Commented:
ok I just seen your note. You will be away. I'll log back in after an hour too. I was wondering if you could see the zip files and make changes to those and then I think I will understand whats going on. It is amazing how this radio button (Which I thought would be easiest ) turned out so difficult.

Again I appreciate you helping me with this. I am getting my feet wet with Ajax so its kinda time consuming.
Thanks
0
 
syedasimmeesaqAuthor Commented:
Also when I tried the code above, it gave me this
Invalid Parameter received

so if you would be kind enough to look at the files I attached and if you could attached the correct version , I will test it out. I have exhausted evey other option :-(

Thanks again.. see you in a bit
0
 
hieloCommented:
do you need to:
A. execute:
$myres = "update survey set PC='". $_GET[$key] ."' where ID=1";

once for every radio button

OR:
B. execute the update above only once and put all the values if the radio buttons into a single field as a comma-separated list of values?
0
 
syedasimmeesaqAuthor Commented:
I need to execute it for evey radio button. There can be only one value in the field. Depending which radio button was choosen it could either be 1, 2, 3, 4, 5,6 or 7

It can not be multiple values
Thanks
0
 
hieloCommented:

// JavaScript Document
// JavaScript Document
<!--
//Browser Support Code
function ajaxFunction(element){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Please update your browser!");
				return false;
			}
		}
	}
	var url="mainsurvey.php?Techsupport=" + encodeURIComponent(document.getElementById('Techsupport').value) 
	+ "&starttime="+encodeURIComponent(document.getElementById('starttime').value) 
	+ "&NumberOfIssues="+encodeURIComponent(document.getElementById('NumberOfIssues').value) 
	+ "&OverSuper="+encodeURIComponent(document.getElementById('OverSuper').value)
	+ "&endtime="+encodeURIComponent(document.getElementById('endtime').value)
	+ "&NumberOfClients="+encodeURIComponent(document.getElementById('NumberOfClients').value)
	+ "&ODate="+encodeURIComponent(document.getElementById('ODate').value)
	+ "&C_L="+(document.getElementById('C_L').checked ? 1 : 0)
	+ "&C_M="+(document.getElementById('C_M').checked ? 1 : 0)
	+ "&C_S="+(document.getElementById('C_S').checked ? 1 : 0)
	+ "&C_SS="+(document.getElementById('C_SS').checked ? 1 : 0)
	+ "&C_A="+(document.getElementById('C_A').checked ? 1 : 0)
	+ "&C_Other="+encodeURIComponent(document.getElementById('C_Other').value)
	+ "&PC[]="+(document.getElementById('PC1').checked ? 1 : 0)
	+ "&PC[]="+(document.getElementById('PC2').checked ? 2 : 0)
	+ "&PC[]="+(document.getElementById('PC3').checked ? 3 : 0)
	+ "&PC[]="+(document.getElementById('PC4').checked ? 4 : 0)
	+ "&PC[]="+(document.getElementById('PC5').checked ? 5 : 0)
	+ "&PC[]="+(document.getElementById('PC6').checked ? 6 : 0)
	+ "&PC[]="+(document.getElementById('PC7').checked ? 7 : 0)
	+ "&PC_n="+encodeURIComponent(document.getElementById('PC_notes').value)
	+ '&element=' + element;
	ajaxRequest.open("GET", url, true);
    ajaxRequest.onreadystatechange = function(){            
      switch(ajaxRequest.readyState) {
        case 4:
        if(ajaxRequest.status!=200) {
            alert("Error:"+ajaxRequest.status); 
        }else{
        	var xml = ajaxRequest.responseXML;
            var updateElement = xml.getElementsByTagName("element")[0];
            document.getElementById('update' + updateElement.firstChild.nodeValue).innerHTML = '<img src="img/greencheck.jpg" height="20" width="20" alt="Updated">';
        }
        break;
 
        default:
            return false;
        break;     
    }
  };	
	ajaxRequest.send(null);
 
 
 
}
function changeColor(obj,color){
obj.style.backgroundColor = color;
}
//-->

Open in new window

0
 
hieloCommented:

<?php 
session_start();
require_once("connect_info.php");
foreach($_GET['PC'] as $val )
{
$myres = "update survey set 
						Techsupport = '".$_GET['Techsupport']."', 
						Starttime = '".$_GET['starttime']."',
						 NumberOfIssues = '".$_GET['NumberOfIssues']."', 
						 OverSuper = '".$_GET['OverSuper']."', 
						 Endtime = '".$_GET['endtime']."', 
						 NumberOfClients = '".$_GET['NumberOfClients']."', 
						 ODate= '".$_GET['ODate']."', 
						
						 C_L = '".$_GET['C_L']."',  
						 C_M = '".$_GET['C_M']."', 
						 C_S = '".$_GET['C_S']."', 
						 C_A = '".$_GET['C_A']."',
						 C_Other  = '".mysql_real_escape_string($_GET['C_Other'])."',  
						 Content_SS= '".$_GET['Content_SS']."',
						 PC= '".$val."', 
						 PC_n= '".$_GET['PC_n']."' 
						    where ID=1";
						 
						 
	$inrec = mysql_query($myres);
}
	header("Content-Type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    echo "<response><element>";
    echo $_GET['element'];
    echo "</element></response>";
    die(); 
	
 
?>

Open in new window

0
 
hieloCommented:
What I gave you is technically correct, I believe the ultimate results will NOT be what you expect. Looking at your where clause you have:
...  where ID=1

Assuming your db currently has only ONE record with ID=1:
ID PC...
1  6

If you  check pc1 and pc2, the foreach will do:
UPDATE ...., PC=1 WHERE ID=1

Then again:
UPDATE ...., PC=2 WHERE ID=1

So ultimately the db will reflect only the last value:
ID PC...
1  2

Even if your db had multiple records initially
ID PC...
1  6
1  7

When you execute the update the first time PC=1, your db will change to:
ID PC
1  1
1  1

When you execute again for PC=2
ID PC
1  2
1  2

I don't know what your db looks like but you need to adjust the where clause so that only the relevant fields/records get updated.
0
 
syedasimmeesaqAuthor Commented:
Hi thank you for posting the codes. The above two files you posted, should I change my current files (that I included in zip format above) to those that you have posted here. Also what about the fields in the form. Do they stay the same or should I change them as I see that you used PC[] array.
Thanks
0
 
syedasimmeesaqAuthor Commented:
I changed eveything as you mentioned. And now it doesn't update any field.
Thanks
0
 
syedasimmeesaqAuthor Commented:
ok its updating other records now...but with the PC radio button field, no matter what I choose, it always number 7 thats checked. What can we do?
Thanks for all your help
0
 
syedasimmeesaqAuthor Commented:
in my form I am doing this
1<input name="PC[]" type="radio"  id ="PC1" value="1" <?php if ($row["PC"]==1) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
2<input name="PC[]" type="radio" id ="PC2" value="2" <?php if ($row["PC"]==2) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
3<input name="PC[]" type="radio" id ="PC3" value="3" <?php if ($row["PC"]==3) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
4<input name="PC[]" type="radio" id ="PC4" value="4"  <?php if ($row["PC"]==4) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
5 <input name="PC[]" type="radio" id ="PC5" value="5" <?php if ($row["PC"]==5) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
6<input name="PC[]" type="radio" id ="PC6" value="6" <?php if ($row["PC"]==6) echo "checked"; ?> onclick="ajaxFunction('PC');" /> : &nbsp;&nbsp;&nbsp;
7 <input name="PC[]" id ="PC7" type="radio" value="7" <?php if ($row["PC"]==7) echo "checked"; ?> onclick="ajaxFunction('PC');" /><div id="updatePC"></div>

is this correct?
Thanks
0
 
hieloCommented:
if you put:
print_r( $_REQUEST['PC']);

IMMEDIATELY AFTER:
require_once("connect_info.php");

do you see the PC values as an array?
0
 
syedasimmeesaqAuthor Commented:
no I don't see anything
0
 
hieloCommented:
>>but with the PC radio button field, no matter what I choose, it always number 7 thats checked. What can we do?
OK, yes. This problem is exactly what I explained above (ID: 22694049). The answer depends on your db design. What does your table look like? It sounds to me like your table should have a PCID field AND and PCValue field (see attached example) and instead of just sending PC[]=1,PC[]=0,PC[]=3,...
you will need to explictly send:
PC1=1, PC2=0,PC3=1

as suggested above(ID: 22689333) since when you send this:
you really don't know which field(s) are zero. Assuming ascending order is NOT the correct approach.
Survey
-------
Techsupport|Starttime|...|PCID|PCValue
...        |...      |...|... |1   | 0
...        |...      |...|... |2   | 1
...        |...      |...|... |3   | 0
 

Open in new window

0
 
syedasimmeesaqAuthor Commented:
but if I do that, it will work as checkbox which means that PC1 can be 1 and PC7 can be 7, where as I want it to be radio button where if user select PC7, then PC7 = 1 and rest are zero. Hope that makes sense. Also PC7 is just the id. The field name is PC. So PC can have the values from 1 to 7. If the user selected PC with id of PC7 then the value of the PC field in the database is 7. If the user checked the field PC with id PC2 then the value of the PC field in db should be 2.
So we do not need 7 PC fields. We just need to change the value of the PC field in the db with the value the user selected from the form.
Hope this makes sense.
Thanks
0
 
syedasimmeesaqAuthor Commented:
In ID 22694049 , you are right. This is how it should be working with the exception of one. The user can not choose 2 PCs at one time. Its a radio button so the user should be able to select only one value. And if the user comes back and select the other value for the field, then the previous value should get over written.
Thanks
0
 
hieloCommented:
>> Its a radio button so the user should be able to select only one value. And if the user comes back and select the other value for the field, then the previous value should get over written.
OK. This clarifies things significantly. The problem is that you are sending the values of all seven radio buttons. This is NOT necessary. All you need is THE checked value. So this would work:
<?php 
session_start();
require_once("connect_info.php");
$myres = "update survey set 
						Techsupport = '".$_GET['Techsupport']."', 
						Starttime = '".$_GET['starttime']."',
						 NumberOfIssues = '".$_GET['NumberOfIssues']."', 
						 OverSuper = '".$_GET['OverSuper']."', 
						 Endtime = '".$_GET['endtime']."', 
						 NumberOfClients = '".$_GET['NumberOfClients']."', 
						 ODate= '".$_GET['ODate']."', 
						
						 C_L = '".$_GET['C_L']."',  
						 C_M = '".$_GET['C_M']."', 
						 C_S = '".$_GET['C_S']."', 
						 C_A = '".$_GET['C_A']."',
						 C_Other  = '".mysql_real_escape_string($_GET['C_Other'])."',  
						 Content_SS= '".$_GET['Content_SS']."',
						 PC= '".$_GET['PC']."', 
						 PC_n= '".$_GET['PC_n']."' 
						    where ID=1";
						 
						 
	$inrec = mysql_query($myres);
	header("Content-Type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    echo "<response><element>";
    echo $_GET['element'];
    echo "</element></response>";
    die(); 
	
 
?>
 
 
IF you send ONLY THE checked item:
...
var url="mainsurvey.php?Techsupport=" + encodeURIComponent(document.getElementById('Techsupport').value) 
	+ "&starttime="+encodeURIComponent(document.getElementById('starttime').value) 
	+ "&NumberOfIssues="+encodeURIComponent(document.getElementById('NumberOfIssues').value) 
	+ "&OverSuper="+encodeURIComponent(document.getElementById('OverSuper').value)
	+ "&endtime="+encodeURIComponent(document.getElementById('endtime').value)
	+ "&NumberOfClients="+encodeURIComponent(document.getElementById('NumberOfClients').value)
	+ "&ODate="+encodeURIComponent(document.getElementById('ODate').value)
	+ "&C_L="+(document.getElementById('C_L').checked ? 1 : 0)
	+ "&C_M="+(document.getElementById('C_M').checked ? 1 : 0)
	+ "&C_S="+(document.getElementById('C_S').checked ? 1 : 0)
	+ "&C_SS="+(document.getElementById('C_SS').checked ? 1 : 0)
	+ "&C_A="+(document.getElementById('C_A').checked ? 1 : 0)
	+ "&C_Other="+encodeURIComponent(document.getElementById('C_Other').value);
	
	var f=document.forms[0];
	for( var i=0, limit=f.PC.length; i < limit; ++i)
	{
		if( f.PC[i].checked )
		{
			url += "&PC=" + f.PC[i].value;
			break;
		}
	}
 
	url+="&PC_n="+encodeURIComponent(document.getElementById('PC_notes').value)
	+ '&element=' + element;
...

Open in new window

0
 
syedasimmeesaqAuthor Commented:
ok but in the php section of file where we use this to check for PC values
foreach($_GET['PC'] as $val ){
}

what if I have another (actually few other) fields  for radio buttons. Lets say that other field name is NC
can I do this
foreach(($_GET['PC'] as $PCval )&&($_GET['NC'] as $NCval )){
}
I tried this but it gives me error 500 on my IE browser.
Thanks

0
 
hieloCommented:
>>ok but in the php section of file where we use this to check for PC values
foreach($_GET['PC'] as $val ){
}

Since you are sending only ONE PC value, you do NOT need the foreach. In your query you would just do:
"UPDATE ... SET ..., PC='{$_REQUEST['PC']}'...";

>>what if I have another (actually few other) fields  for radio buttons
You once again need to send just ONE value for each of those items. The value of THE checked item. Since you are doing this via ajax, YOU (the developer) need to assume the role of the browser. When you have a set/group or radio buttons, the browser will send only the value of the checked item in the radio group. It does NOT send zero for the unchecked items, like you were doing initially.
0
 
syedasimmeesaqAuthor Commented:
if I do this
UPDATE ... SET ..., PC='{$_REQUEST['PC']}

it gives me Error 500 popup on IE browser.
but if I use foreach loop then it seems to work. So my question stay the same as above. What can I do to send another field lets say NC. I changed the ajax function to this and added NC to it


switch( true )
      {
            case document.getElementById('PC1').checked: url +="&PC[]=1"; break;
            case document.getElementById('PC2').checked: url +="&PC[]=2"; break;
            case document.getElementById('PC3').checked: url +="&PC[]=3"; break;
            case document.getElementById('PC4').checked: url +="&PC[]=4"; break;
            case document.getElementById('PC5').checked: url +="&PC[]=5"; break;
            case document.getElementById('PC6').checked: url +="&PC[]=6"; break;
            case document.getElementById('PC7').checked: url +="&PC[]=7"; break;
      }
 
      url+="&PC_notes="+encodeURIComponent(document.getElementById('PC_notes').value);
      switch( true )
      {
            case document.getElementById('NC1').checked: url +="&NC[]=1"; break;
            case document.getElementById('NC2').checked: url +="&NC[]=2"; break;
            case document.getElementById('NC3').checked: url +="&NC[]=3"; break;
            case document.getElementById('NC4').checked: url +="&NC[]=4"; break;
            case document.getElementById('NC5').checked: url +="&NC[]=5"; break;
            case document.getElementById('NC6').checked: url +="&NC[]=6"; break;
            case document.getElementById('NC7').checked: url +="&NC[]=7"; break;
      }
 
      url+="&NC_notes="+encodeURIComponent(document.getElementById('NC_notes').value)
      
      
      
      
      
      + '&element=' + element;


however what do I do for foreach loop?

Thanks
0
 
syedasimmeesaqAuthor Commented:
I tried this

foreach($_GET['PC'] as $pcval )
{
foreach($_GET['NC'] as $ncval )
{
$myres = "update survey set  
....
PC= '".$pcval."',
PC_n= '".$_GET['PC_n']."'
 NC= '".$ncval."',
NC_notes= '".$_GET['NC_n']."'
                                     
 where ID=1";
                                     
                                     
                                     
      $inrec = mysql_query($myres);
}
}
but it doesn't update any records then, however it doesn't give that nasty error 500 either.
Thanks

0
 
syedasimmeesaqAuthor Commented:
actually that worked. I had a problem in the query. missing , thats why it wasn't updating. Thank you for all your help. as always you had been great.
Thanks again
0
 
syedasimmeesaqAuthor Commented:
Excellent excellent advices as always
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 37
  • 19
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now