0

我有一个带有选择的表格。这些选项来自数据库并显示在选择中。

数据库中的列是:

id, date, title, body.

在选择时,我只显示日期和标题。在同一页面中,我有一个表格:

 date -> input text
 title -> input text
 body -> text area

我希望当用户单击选择中的选项时,下面的字段将使用从包含数据库数据的选定选项中保留的内容进行更新。

现在我不知道如何获取以下字段中的数据。我的想法是获取下拉选项的索引。这可能吗?

编辑完整代码:

<html>
<body>

<div class="normal">
<table style="width:100%;height:100%;" border="1">
    <tr>
        <td width="174"><strong>geste n</strong></td>

        <td width="505" valign="middle" height="24">

            <input type="radio" name="n" id="new" value="nuova" onchange="shown(this.id);" <?php if($_POST['n'] == "nuova"){echo "CHECKED";} ?> />Nuova n
            <input type="radio" name="n" id="mod" value="modifica" onchange="shown(this.id);" <?php if($_POST['n'] == "modifica"){echo "CHECKED";} ?> />Modifica n
                <select id="sceltannascosta" style="display:none">
                    <?php
                        include("/login/flock/sql.php");
                        $conn = mysqli_connect($host, $user, $pw, $db) or die("Impossibile connettersi"); 
                        $a = "SELECT * FROM n ORDER BY id DESC";
                        $b = mysqli_query($conn, $a);                       
                            print '<option></option>';                      
                        while ($c = $b->fetch_object()) {
                            print '<option>'.$c->id.$c->data.$c->title.$c->body.'</option>';
                        }
                        $b->close();
                        $conn->close;      
                    ?>    
                </select>

                <select id="sceltan" style="display:none" onChange="showEntry(this,this.value);setndamod(this);">
                    <?php
                        include("/login/flock/sql.php");
                        $conn = mysqli_connect($host, $user, $pw, $db) or die("Impossibile connettersi"); 
                        $a = "SELECT * FROM n ORDER BY id DESC";
                        $b = mysqli_query($conn, $a);                       
                            print '<option></option>';                      
                        while ($c = $b->fetch_object()) {
                            print '<option>'.$c->data.' - '.$c->title.'</option>';
                        }
                        $b->close();
                        $conn->close;      
                    ?>  
                </select>                
            <input type="submit" id="botenascostogeste" class="button">       
        </form>
        </td>
    </tr>
</table>
</div>

<p>&nbsp;</p>

<div class="normal" id="c" style="display:none">
<table style="width:100%;height:100%;">
  <tr>
      <td colspan="3" border="0">
        <strong class="confirm">c nuova n</strong>
      </td>
  </tr>

  <tr>
    <td width="107" align="right">
        <strong>Data</strong>
    </td>
    <td colspan="2">
        <form name="nuovan" method="post" action="cht.php?c=yes">
            <input name="datan" type="text" maxlength="10" size="8" value="<?php echo date(d.".".m.".".Y) ?>"> gg.mm.aaaa
    </td>
  </tr>

  <tr>
      <td align="right">
        <strong>Titolo</strong>
      <td width="360">
                <input name="titolon" type="text" maxlength="50" size="50" value="<? echo htmlspecialchars("titolo n - lunghezza massima 50 caratteri") ?>" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')">
      </td>
      <td width="522">
          <b><span id=myCounter>6</span></b> caratteri rimanenti per il titolo</font>
      </td>
  </tr>

  <tr>
    <td colspan="3">
                <textarea name="teston"><?php if(isset($_POST['teston'])){echo $_POST['teston'];}else{echo 'Testo n con stile personalizzato';} ?></textarea>
                <script>
                    CKEDITOR.replace('teston');
                </script>
    </td>
  </tr>

  <tr>
    <td align="right">
        <input type="submit" name="c" class="button" onClick="document.getElementById('botenascostogeste').click();">
    </form>
    </td>
    <td colspan="2">
    <?php
        $c = $_GET['c'];                
        $titolon = $_POST['titolon'];
        $teston = $_POST['teston'];
        $datascritta = $_POST['datan'];

        if($c == 'yes'){                        
            if($_POST['titolon'] == NULL || $_POST['teston'] == NULL || $_POST['datan'] == NULL){
                echo '<img src="'.$imagen.'" style=width:"' . $width . 'px;height:' . $height . 'px;">';
            } else {

                function verificadata($datascritta) { 
                    list($dd,$mm,$yyyy)=explode(".",$datascritta); 
                    if (is_numeric($dd) && is_numeric($mm) && is_numeric($yyyy)) 
                    { 
                        return checkdate($mm,$dd,$yyyy); 
                    } 
                    return false;            
                }

                if(verificadata($datascritta)){
                    $connessione = mysql_connect ($host, $user, $pw) or     //connect to the database 
                        die('<img src="'.$imagen.'" style=width:"' . $width . 'px;height:' . $height . 'px;">');                                                    
                    mysql_select_db ($db); //select the database                
                    mysql_query("INSERT INTO n(data, title, body) VALUES ('$datascritta', '$titolon', '$teston')");
                    mysql_close($connessione);
                    echo ('<img src="'.$imagey.'" style=width:"' . $width . 'px;height:' . $height . 'px;"> Ok'); // It worked.
                } else { die('<img src="'.$imagen.'" style=width:"' . $width . 'px;height:' . $height . 'px;">');                                                   
                }
            }
        }
    ?>    
    </td>
</table>
</div>

<div class="normal" id="modifica" style="display:none">
<table style="width:100%;height:100%;">
  <tr>
      <td colspan="3" border="0">
        <strong class="confirm">Modifica n</strong>
      </td>
  </tr>

  <tr>
    <td width="107" align="right">
        <strong>Data</strong>
    </td>
    <td colspan="2">
        <form name="modifican" method="post" action="cht.php?modifica=yes">
            <input name="idmodifican" type="text" style="display:none" value="INSERIRE ID DI MODIFICA DEL DATABASE QUI">
            <input name="datanmodificata" type="text" maxlength="10" size="8" value="POST DATE FROM THE n TO MODIFY SELECTED"> gg.mm.aaaa
    </td>
  </tr>

  <tr>
      <td align="right">
        <strong>Titolo</strong>
      </td>
      <td width="360">
                <input name="modificatitolo" type="text" maxlength="50" size="50" value="POST TITLE FROM THE n TO MODIFY SELECTED" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')">
      </td>
      <td width="522">
          <b><span id=myCounter>50</span></b> caratteri rimanenti per il titolo</font>
      </td>
  </tr>

  <tr>
    <td colspan="3">
                <textarea name="modificatesto">POST BODY FROM THE n TO MODIFY SELECTED</textarea>
                <script>
                    CKEDITOR.replace('modificatesto');
                </script>
    </td>
  </tr>

  <tr>
    <td align="right">
        <input type="submit" name="modifica" class="button">
    </form>
    </td>
    <td colspan="2">
    PHP I HAVE TO DO TO UPDATE THE n ON THE DATABASE (I'm able to do this part).
    </td>
</table>
</div>  

</body>
4

1 回答 1

0

为此,您需要进行 ajax 调用。
然后使用jquery根据选择字段中选择的值设置字段,您可以设置其他字段的值尝试根据需要修改它

<body>
    <form>
        <select id="sel_opt">
        <?php 
          $i = 1;
          foreach($valueformdb as $value)
          {
              echo "<option class=\"option\" id=\"opt_$i\">";
              echo $value;
              echo '</option>';
              $i++;
          }

        ?>    
        </select>
       <input type="text" id="txt_date" value="" />
       <input type="text" id="txt_title" value="" />
       <textarea id="ta_body"></textarea>
    </form>
    <script type="text/javascript">
    $(document).ready(function(){
       $('.sel_opt').on('change', function(){
           var id = $(this).attr('id');
           $.ajax({
          type:'POST',
          dataType: 'json',
          url:" ",// path of file to handle ajax request
          data:'option='+id, // this will be posted on the page which handle the ajax and give back the data from db
          success:function(data){
              $('#txt_date').val(data[date]);// ajax respond the array containg the value its advised to use json
              $('#txt_title').val(data[title]);
             }
          });
       });
    });
    </script>
</body>

这不是您需要稍微修改的确切代码,因为我不知道您要实现的确切目标

于 2013-01-25T16:34:41.823 回答