SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    PHP Code:
    $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 Code:
    <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 Code:
    <?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];
        }
    ?>

  2. #2
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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;
    }
    ?>
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    HTML Code:
    <script>
    $(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>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>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()) >= 0;
    	}
    });
    function showdoc()
    {
      document.getElementById("HiddenDoc").style.display = 'block';
    }
    
    function gtdoc()
    {
      for(var i=0; i < 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 +  " \n "; 
               sCode = sCode +  " \n "; 
               
               document.myform.sqlCode.value = sCode; 
               iNextFileID = document.myform.NextFileIndex.value * 1; 
               iNextFileID++; 
               document.myform.NextFileIndex.value = iNextFileID.toString(); }
    </script>
    
    <body>
    <form name="myform" actiopn="<?php $_PHP_SELF ?>" method="get">
             <table width='100%'>
         <tr>
          <td>DATE</td>
          <td>USER</td>
          <td>File</td>
          <td>Object</td>
          <td>Next ID</td>
          <td>New File Name</td>
        </tr>
    
        <tr>
          <td><input type="text" name="DateValue" value="" size=10 readonly="true"></input></td>
          <td><input type="text" name="WindowsUser" value="" size=12></input></td>
          <td><input type="text" name="NextFileIndex" value="<?php echo $row2[0];  ?>" size=5 readonly="true"></input></td>
          <td><input type="text" name="NextObjectIndex" value="<?php echo $row3[0];  ?>" size=5 readonly="true"></input></td>
          <td><input type="text" name="UniqueObjects" value="<?php echo $row6[0];  ?>" size=5 readonly="true"></input></td>
          <td><input type="text" name="RevisedFileName" value="" size=130 readonly="true"></input></td>
        </tr>
         </table>
    <textarea name="sqlCode" value="" rows="5" cols="100" wrap="soft" readonly="true"></textarea>  
    
    <label for="kwd_search">Search:</label> 
    <input type="text" id="kwd_search" onChange="showdoc();"  value=""/>
    
    
    <div id="HiddenDoc" style="display:none;">
    <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>";   
        }
    ?>
    </table>
    </div>
    PHP Code:
    <div id="HiddenDoc2" style="display:none;"> 
    <?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 
    "<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];
        }
    }
    ?>  

    </div>               

    <input type="button" value="SUBMIT FOR APPROVAL" onClick="updateFILE()"/></input>  </br>
    </div>
    </body>

  4. #4
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also if I assign a value to $ObjIndex such as $ObjIndex = '1407'; the hidden table appears with all the info for value '1407'

  5. #5
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    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.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  6. #6
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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
    Code:
    <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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •