PHP Switch to get radio input value

I have a query ($myQ1) that selects information from the database and put it into an array list with a radio button selection. The Value and the name of the document are defined in this radio button. I want to store the value from the selected radio button into a variable “$ObjIndex” and that will give the information needed to run the next three queries. When I select one of the radio buttons I want the table below the list to appear with the information from the query. I can do this using a switch statment but I dont know how. Also, if I dont need it I dont have to have $myQ9 it pulls the same information from $myQ1. Also Note when I set $ObjIndex to a real index number such as “$ObjIndex = ‘1407’” it does exactly what I need it to do. So if I can just get it to do like “$ObjIndex = ‘$tog’” and $tog is the value from the selected index. it should work… I think
Please excuse my lingo Im very new to this Also all of this is on one page


$myQ1 = mysql_query("SELECT `iDocuments`.`File`.`FileIndex`, `iDocuments`.`File`.`FileName`, `iDocuments`.`Object`.`ObjectIndex`, `iDocuments`.`Object`.`ObjectName`, `iDocuments`.`Version`.`Version`, `iDocuments`.`File`.`UploadDate` FROM `iDocuments`.`Version` INNER JOIN `iDocuments`.`Object` ON `iDocuments`.`Object`.`ObjectIndex` = `iDocuments`.`Version`.`ObjectIndex` JOIN `iDocuments`.`File` ON `iDocuments`.`Version`.`FileIndex` = `iDocuments`.`File`.`FileIndex` WHERE `Object`.`ObjectStatus` = '1' AND `Version`.`Version` = ( SELECT MAX(`Version`) FROM `iDocuments`.`Version` WHERE `Version`.`ObjectIndex` = `Object`.`ObjectIndex` ) ORDER BY `Object`.`ObjectName` ASC");
if (!$myQ1) 
       {  
          echo 'Could not run query 1: ' . mysql_error();
       } 
else 
       { 
	  $row1 = mysql_query($myQ1);   
?>


<html>
<head>
</head>
<form name="myform" actiopn="<?php $_PHP_SELF ?>" method="get"> 
<table id="my-table"><tr><textarea name="showit" id="showit" value=""></textarea>


<?php
   while( $row1 = mysql_fetch_array($myQ1) ) 
    {
     echo "<td><input type='radio' name='tog' id='tog' onClick='gtdoc()' value='$row1[2]'>$row1[3]</input></td></tr>";
    }

if(isset($_GET['tog'])) {
 $tog = $_GET['tog'];
   switch($row1) {
     case $tog:
       $tog = $ObjIndex;
       break; }
}
echo "</table>";


$myQ7 = mysql_query("SELECT DISTINCT `iIndex`.`TypeDef`.`Name` FROM `iIndex`.`TypeIndex` JOIN `iIndex`.`TypeDef` ON `iIndex`.`TypeDef`.`Index` = `iIndex`.`TypeIndex`.`TypeID` WHERE `iIndex`.`TypeIndex`.`ObjIndex` = '$ObjIndex' ");
$myQ8 = mysql_query("SELECT DISTINCT `iIndex`.`TopicDef`.`Name` FROM `iIndex`.`TopicIndex` JOIN `iIndex`.`TopicDef` ON `iIndex`.`TopicDef`.`Index` = `iIndex`.`TopicIndex`.`TopicID` WHERE `iIndex`.`TopicIndex`.`ObjIndex` = '$ObjIndex' ");
$myQ9 = mysql_query("SELECT `iDocuments`.`File`.`FileIndex`, `iDocuments`.`File`.`FileName`, `iDocuments`.`Object`.`ObjectIndex`, `iDocuments`.`Object`.`ObjectName`, `iDocuments`.`Version`.`Version`, `iDocuments`.`File`.`UploadDate` FROM `iDocuments`.`Version` INNER JOIN `iDocuments`.`Object` ON `iDocuments`.`Object`.`ObjectIndex` = `iDocuments`.`Version`.`ObjectIndex` JOIN `iDocuments`.`File` ON `iDocuments`.`Version`.`FileIndex` = `iDocuments`.`File`.`FileIndex` WHERE `Object`.`ObjectStatus` = '1' AND `Version`.`Version` = ( SELECT MAX(`Version`) FROM `iDocuments`.`Version` WHERE `Version`.`ObjectIndex` = `Object`.`ObjectIndex` ) AND `Object`.`ObjectIndex` = '$ObjIndex'");
if (!$myQ7) 
       {  
          echo 'Could not run query 7: ' . mysql_error(); 
       }
if (!$myQ8) 
       {  
          echo 'Could not run query 8: ' . mysql_error(); 
       }
if (!$myQ9) 
       {  
          echo 'Could not run query 9: ' . mysql_error(); 
       }
else 
       { 
    	  $row7 = mysql_query($myQ7);
    	  $row8 = mysql_query($myQ8);
	  $row9 = mysql_query($myQ9);
          $aID = 0;
       }

  while( $row9 = mysql_fetch_array($myQ9) ) 
   {
      echo "<table name='doclist'>           
      <tr><td>FileID</td><td><input name='DocFileID' type='text' readonly='true' value='$row9[0]'></input></td></tr> 
      <tr><td>File Name</td><td><input name='DocFileName' type='text' readonly='true' value='$row9[1]'></input></td></tr> 
      <tr><td>ObjectID</td><td><input name='DocID' type='text' readonly='true' value='$row9[2]'></input></td></tr>  
      <tr><td>Object Name</td><td><input name='DocName' type='text' readonly='true' value='$row9[3]'></input></td></tr>  
      <tr><td>Current Version</td><td><input name='CurrVer' type='text' readonly='true' value='$row9[4]'></input></td></tr>
      <tr><td>Update Date</td><td><input name='lstupdate' type='text' readonly='true' value='$row9[5]'></input></td> </tr>
      </table>";
   }
   while( $row8 = mysql_fetch_array($myQ8) ) 
    {
      echo "<input type='checkbox' name='topicdef'>$row8[0]</input>";
    }
   while( $row7 = mysql_fetch_array($myQ7) ) 
    {
      echo $row7[0];
    }
?>  

It’s possible I’ve misunderstood your requirement, but here goes.

For $ObjIndex to be intialized the form must be submitted. It won’t have a value when the page first loads. On submit, the page will reload and your form values will be retrievable. Once the form is submitted, you can pick up the value with $ObjIndex = $_GET[‘showit’]; or what ever value should be picked up. As you already have with $tog.

Your form contains an onclick event, which is javascript - what does function gtdoc() do? Is it loading the chosen document into the textarea?

Also, I can’t see a “submit” button in your code.

If your not familiar with the switch statement, you can use a series of if statements. The following two examples are two different ways of writing exactly the same thing:

<?php
if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}
?>

What this form will eventually do is create a SQL code to add and update documents to our intranet. I want to be able to fill the form before the submit button. The submit button is function updateFILE(). The table that is being displayed with the data is in a hidden div the function gtdoc() is just checking to see if the radio button is checked and then displaying the div. In order the while loop is called but inside a hidden div, there is a text box that onChange will appear after its searches the table that the while loop has formed. Then it will create the sql code from the selected values.


&lt;script&gt;
$(document).ready(function(){
	// Write on keyup event of keyword input element
	$("#kwd_search").keyup(function(){
		// When value of the input is not blank
		if( $(this).val() != "")
		{
			// Show only matching TR, hide rest of them
			$("#my-table tbody&gt;tr").hide();
			$("#my-table td:contains-ci('" + $(this).val() + "')").parent("tr").show();
		}
		else
		{
			// When there is no input or clean again, show everything back
			$("#my-table tbody&gt;tr").hide();
		}
	});

});
// jQuery expression for case-insensitive filter
$.extend($.expr[":"], 
{
    "contains-ci": function(elem, i, match, array) 
	{
		return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) &gt;= 0;
	}
});
function showdoc()
{
  document.getElementById("HiddenDoc").style.display = 'block';
}

function gtdoc()
{
  for(var i=0; i &lt; document.myform.tog.length; i++)
  {
    if(document.myform.tog[i].checked)
    {
	document.getElementById("HiddenDoc2").style.display = 'block' 
    } 
  }
}
function updateFILE()  {
sCode = document.myform.sqlCode.value; 
           iFileID = document.myform.DocFileID.value; 
           iObjectID = document.myform.DocID.value; 
           iCurrentVersion = document.myform.CurrVer.value;
	   iFileName = document.myform.DocFileName.value; 
	   iObjectName = document.myform.DocName.Value

           iDashPosition = iFileName.search( "-" + iFileID); 
           iDatePosition = iDashPosition - 8; 
           iDotsPosition = iDashPosition + iFileID.length + 1;
            
           iDateYYYYMMDD = document.myform.DateValue.value; 
           iDateYYYYMMDD = iDateYYYYMMDD.replace(/-/g, "");

           iFileName = iFileName.substring(0, iDatePosition) + iDateYYYYMMDD + "-" + document.myform.NextFileIndex.value + iFileName.substring(iDotsPosition, iFileName.length);
           document.myform.RevisedFileName.value = iFileName;
            
           sCode = sCode +  "UPDATE `iDocuments`.`File` SET `FileStatus` = '0' WHERE `FileIndex` = ' " + iFileID +  "'; "; 
           sCode = sCode +  "INSERT INTO `iDocuments`.`File` (`FileIndex`, `FileStatus`, `FileName`, `FileDesc`, `UploadDate`, `UploadTime`, `UploadUserID`) VALUES (' " + document.myform.NextFileIndex.value +  "', '1', ' " + iFileName +  "', '', ' " + document.myform.DateValue.value +  "', '12:00:00', '1'); "; 
           sCode = sCode +  "INSERT INTO `iDocuments`.`Version` (`Index`, `ObjectIndex`, `Version`, `FileIndex`) VALUES ('', ' " + iObjectID +  "', ' " + iCurrentVersion.toString() +  "', ' " + document.myform.NextFileIndex.value +  "'); "; 
           sCode = sCode +  "INSERT INTO `iUpdates`.`Updates` (`UpdateID`, `UpdateDate`, `InputDateTime`, `InputUserID`, `InputUserName`, `UpdateTitle`, `UpdateText`, `LinkURL`, `LinkWindowID`) VALUES ('', ' " + document.myform.DateValue.value +  "', ' " + document.myform.DateValue.value +  " 12:00:00', '2', ' " + document.myform.WindowsUser.value +  "', ' " + iObjectName +  "', '', 'download.php?object= " + iObjectID +  "', '2'); "; 
           sCode = sCode +  " \
 "; 
           sCode = sCode +  " \
 "; 
           
           document.myform.sqlCode.value = sCode; 
           iNextFileID = document.myform.NextFileIndex.value * 1; 
           iNextFileID++; 
           document.myform.NextFileIndex.value = iNextFileID.toString(); }
&lt;/script&gt;

&lt;body&gt;
&lt;form name="myform" actiopn="&lt;?php $_PHP_SELF ?&gt;" method="get"&gt;
         &lt;table width='100%'&gt;
     &lt;tr&gt;
      &lt;td&gt;DATE&lt;/td&gt;
      &lt;td&gt;USER&lt;/td&gt;
      &lt;td&gt;File&lt;/td&gt;
      &lt;td&gt;Object&lt;/td&gt;
      &lt;td&gt;Next ID&lt;/td&gt;
      &lt;td&gt;New File Name&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&lt;input type="text" name="DateValue" value="" size=10 readonly="true"&gt;&lt;/input&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="WindowsUser" value="" size=12&gt;&lt;/input&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="NextFileIndex" value="&lt;?php echo $row2[0];  ?&gt;" size=5 readonly="true"&gt;&lt;/input&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="NextObjectIndex" value="&lt;?php echo $row3[0];  ?&gt;" size=5 readonly="true"&gt;&lt;/input&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="UniqueObjects" value="&lt;?php echo $row6[0];  ?&gt;" size=5 readonly="true"&gt;&lt;/input&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="RevisedFileName" value="" size=130 readonly="true"&gt;&lt;/input&gt;&lt;/td&gt;
    &lt;/tr&gt;
     &lt;/table&gt;
&lt;textarea name="sqlCode" value="" rows="5" cols="100" wrap="soft" readonly="true"&gt;&lt;/textarea&gt;  

&lt;label for="kwd_search"&gt;Search:&lt;/label&gt; 
&lt;input type="text" id="kwd_search" onChange="showdoc();"  value=""/&gt;


&lt;div id="HiddenDoc" style="display:none;"&gt;
&lt;table id="my-table"&gt;&lt;tr&gt;&lt;textarea name="showit" id="showit" value=""&gt;&lt;/textarea&gt;
&lt;?php
   while( $row1 = mysql_fetch_array($myQ1) ) 
    {
     echo "&lt;td&gt;&lt;input type='radio' name='tog' id='tog' onClick='gtdoc()'  value='$row1[2]'&gt;$row1[3]&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;";   
    }
?&gt;
&lt;/table&gt;
&lt;/div&gt;


&lt;div id="HiddenDoc2" style="display:none;"&gt; 
&lt;?php
foreach($tog as $ObjIndex) {
$myQ7 = mysql_query("SELECT DISTINCT `iIndex`.`TypeDef`.`Name` FROM `iIndex`.`TypeIndex` JOIN `iIndex`.`TypeDef` ON `iIndex`.`TypeDef`.`Index` = `iIndex`.`TypeIndex`.`TypeID` WHERE `iIndex`.`TypeIndex`.`ObjIndex` = '$ObjIndex' ");
$myQ8 = mysql_query("SELECT DISTINCT `iIndex`.`TopicDef`.`Name` FROM `iIndex`.`TopicIndex` JOIN `iIndex`.`TopicDef` ON `iIndex`.`TopicDef`.`Index` = `iIndex`.`TopicIndex`.`TopicID` WHERE `iIndex`.`TopicIndex`.`ObjIndex` = '$ObjIndex' ");
$myQ9 = mysql_query("SELECT `iDocuments`.`File`.`FileIndex`, `iDocuments`.`File`.`FileName`, `iDocuments`.`Object`.`ObjectIndex`, `iDocuments`.`Object`.`ObjectName`, `iDocuments`.`Version`.`Version`, `iDocuments`.`File`.`UploadDate` FROM `iDocuments`.`Version` INNER JOIN `iDocuments`.`Object` ON `iDocuments`.`Object`.`ObjectIndex` = `iDocuments`.`Version`.`ObjectIndex` JOIN `iDocuments`.`File` ON `iDocuments`.`Version`.`FileIndex` = `iDocuments`.`File`.`FileIndex` WHERE `Object`.`ObjectStatus` = '1' AND `Version`.`Version` = ( SELECT MAX(`Version`) FROM `iDocuments`.`Version` WHERE `Version`.`ObjectIndex` = `Object`.`ObjectIndex` ) AND `Object`.`ObjectIndex` = '$ObjIndex'");
if (!$myQ7) 
       {  
          echo 'Could not run query 7: ' . mysql_error(); 
       }
if (!$myQ8) 
       {  
          echo 'Could not run query 8: ' . mysql_error(); 
       }
if (!$myQ9) 
       {  
          echo 'Could not run query 9: ' . mysql_error(); 
       }
else 
       { 
    	  $row7 = mysql_query($myQ7);
    	  $row8 = mysql_query($myQ8);
	  $row9 = mysql_query($myQ9);
          $aID = 0;
       }

  while( $row9 = mysql_fetch_array($myQ9) ) 
   {
      echo "&lt;table name='doclist'&gt;           
      &lt;tr&gt;&lt;td&gt;FileID&lt;/td&gt;&lt;td&gt;&lt;input name='DocFileID' type='text' readonly='true' value='$row9[0]'&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt; 
      &lt;tr&gt;&lt;td&gt;File Name&lt;/td&gt;&lt;td&gt;&lt;input name='DocFileName' type='text' readonly='true' value='$row9[1]'&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt; 
      &lt;tr&gt;&lt;td&gt;ObjectID&lt;/td&gt;&lt;td&gt;&lt;input name='DocID' type='text' readonly='true' value='$row9[2]'&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;  
      &lt;tr&gt;&lt;td&gt;Object Name&lt;/td&gt;&lt;td&gt;&lt;input name='DocName' type='text' readonly='true' value='$row9[3]'&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;  
      &lt;tr&gt;&lt;td&gt;Current Version&lt;/td&gt;&lt;td&gt;&lt;input name='CurrVer' type='text' readonly='true' value='$row9[4]'&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;Update Date&lt;/td&gt;&lt;td&gt;&lt;input name='lstupdate' type='text' readonly='true' value='$row9[5]'&gt;&lt;/input&gt;&lt;/td&gt; &lt;/tr&gt;
      &lt;/table&gt;";
   }
   while( $row8 = mysql_fetch_array($myQ8) ) 
    {
      echo "&lt;input type='checkbox' name='topicdef'&gt;$row8[0]&lt;/input&gt;";
    }
   while( $row7 = mysql_fetch_array($myQ7) ) 
    {
      echo $row7[0];
    }
}
?&gt;  

&lt;/div&gt;               

&lt;input type="button" value="SUBMIT FOR APPROVAL" onClick="updateFILE()"/&gt;&lt;/input&gt;  &lt;/br&gt;
&lt;/div&gt;
&lt;/body&gt;

Also if I assign a value to $ObjIndex such as $ObjIndex = ‘1407’; the hidden table appears with all the info for value ‘1407’

Questions:
What is supposed to happen when the page first loads? As it stands, I think only the radio buttons will on the page.

And I’m not sure what’s going on here:

if(isset($_GET['tog'])) {
 $tog = $_GET['tog'];
   switch($row1) {
     case $tog:
       $tog = $ObjIndex;
       break; }
}

On initial page load, $tog and $ObjIndex will have no value unless the page url includes some parameters? However, $ObjIndex is not initialized. So that will always be without a value.

When the page loads there will be radio buttons when a radio button is clicked I want it to display the query in a table from the selected radio button.

From what I can understand, when a radio button is clicked (not the form submitted) a call to the js function gtdoc() is called which does some Ajaxian show/hide.

If the output of gtdoc() is to be variable, then why would you not send in the variable as an argument?

Take this simple htm page:

radiotojs.htm


<script>
function gtdoc(arg){
alert(arg);
alert("now call document make_my_doc.php?type=" + arg);
}

</script>

<input type=radio onclick=gtdoc('one'); name=test value='one'> one <br />
<input type=radio onclick=gtdoc('two'); name=test value='two'> two <br />

Is that what you want to start off with?

Take a variable in JS and shift it to a PHP script which then reacts to the GET string by analysing $_GET[‘type’], buiding an sql query and then returning the output.