2014-01-18 34 views
0

下午好。混合使用jquery和表格

我想提交一個表單到PHP(這將發送到MySQL數據庫),問題是,我beeing能夠提交文本描述和文本內容,但類別和子類別是由腳本選擇,我不能訪問它們。 我正在傳遞我的代碼。我是jquery和php的新手。我花了很多時間試圖解決它,但沒有成功。提前致謝。

 <?php require_once ('../../includes/select.php');?> 
    <?php 
    if (!$_POST['descricao'] == "") { 
$sql = "INSERT INTO conteudos(descricao,conteudo,categorias_id,subcategorias_id)      VALUES('" . $_POST['descricao'] ."','" .$_POST['conteudo'] ."','" . $_POST['categoria'] ."','" . $_POST['subcategoria'] ."');"; 
$sqlconf = mysql_query($sql); 
if (!empty($sqlconf)){ 
    $message = "O novo conteudo foi criado com sucesso."; 
} 
else { 
    $message = "Erro na criação do conteudo, tente novamente."; 
} 
    } 
    ?> 

    <script type="text/javascript"> 
      $(document).ready(function(){ 

     $("select#subcategoria").attr("disabled","disabled"); 
     $("select#categoria").change(function(){ 
     $("select#subcategoria").attr("disabled","disabled"); 
     $("select#subcategoria").html("<option>aguarde...</option>"); 
     var id = $("select#categoria option:selected").attr('value'); 
     $.post("selec_subcategoria.php", {id:id}, function(data){ 
      $("select#subcategoria").removeAttr("disabled"); 
      $("select#subcategoria").html(data); 
     }); 
    }); 
    $("#seleciona").submit(function(){ 
     var desc = $("#descricao").text(); 
     var cont = $("#conteudo").text(); 
     var cat = $("select#categoria option:selected").attr('value'); 
     var subcat = $("select#subcategoria option:selected").attr('value'); 
     event.preventDefault(); 
     $("#result").html(''); 
     //var values = $(this).serialize(); 
     if(cat>0 && subcat>0) 
     { 
      $.ajax({ 
       url: "index.php?pagina=gere_conteudos.php", 
       type: 'POST', 
       data: $("#seleciona").serialize(), 
       success: function(){ 
        alert("success"); 
        $("#result").html('Submitted successfully'); 
       }, 
       error:function(){ 
        alert("failure"); 
        $("#result").html('There is error while submit'); 
       } 
      }); 
      /*$.post('index.php?pagina=gere_conteudos.php', { descricao: desc}); 
      $.post('index.php?pagina=gere_conteudos.php', { conteudo: cont}); 
      $.post('index.php?pagina=gere_conteudos.php', { categoria: cat}); 
      $.post('index.php?pagina=gere_conteudos.php', { subcategoria: subcat}); 
      //$("#result").html('A sua escolha: '+result);*/ 
      $("#teste").html(desc); 
      $("#teste2").html(cont); 
     } 
     else 
     { 

      $("#result").html("Tem de escolher todas as opções disponiveis!"); 

     } 
     //return false; 
    }); 
}); 
     </script> 

    <h2 class="title"><a href="#">SIB - Gestão de conteudos</a></h2> 
    <p class="meta">Domingo, Dezembro 03, 2013 7:27 AM Posted by <a href="#">Someone</a></p> 
    <div class="entry"> 

<div class="tabelas" > 
    <p>Aqui deverá criar e gerir as publicações de conteudos para o seu sistema de informação de balcões. Só após a criação das categorias é que poderá criar as publicações de conteudos.</p> 


    <?php if (!empty($message)) { 
     echo "<p class=\"message\">" . $message . "</p>"; 
    } 
    ?> 
    <form id="seleciona"> 

     <table> 
      <tr> 
       <td>Descrição para a publicação a criar:</td> 
       <td> 
        <input type="text" id="descricao" name="descricao" maxlength="30" value="<?php echo htmlentities($descricao); ?>" /></td> 
      </tr> 
      <tr> 
       <td>Texto da publicação:</td> 
       <td> 
        <textarea id="conteudo" name="conteudo" rows="15" cols="55" ="<?php echo htmlentities($conteudo); ?>"> </textarea></td> 
      </tr> 
      <tr> 
      <tr> 
       <td>Escolha de categoria:</td> 
       <td> 
       <select id="categoria"> 
       <?php print $opt->ShowCategoria(); ?> 
       </select> 
      </tr> 
      <tr> 
       <td>Escolha de subcategoria:</td> 
       <td> 
       <select id="subcategoria"> 
        <option value="0">Escolha...</option> 
       </select> 
       </td> 
       </tr> 
      <tr> 
       <td colspan="2"> 
        <input type="submit" name="submit" value="Criar conteudo" /> 
       </td> 
      </tr> 
     </table> 
    </form> 
    <div id="teste"></div> 
</div> 

回答

0

問題無關與動態值;您忘記了放置name屬性,以便在使用data: $("#seleciona").serialize(),對錶單進行序列化時不會將它們包括在內。

變化:

<select id="categoria"> 

到:

<select id="categoria" name="categoria"> 

和:

<select id="subcategoria"> 

到:

<select id="subcategoria" name="subcategoria"> 
+1

謝謝,你救了我的一天,就是這樣。我怎麼沒注意到! – Saguim

0

只有在提交表單時,具有name屬性的表單元素纔會傳遞其值。您需要在輸入元素中添加name屬性。