【问题标题】:How to Post Input & Select type in single form如何以单一形式发布输入和选择类型
【发布时间】:2017-04-01 23:12:44
【问题描述】:

在 posting.php 文件中我写了下面的代码。在单一表单中,我同时包含输入类型 [文本字段] 和选择 [选项] 类型,当我提交表单时,我收到错误 Notice: Undefined index: designerorder_id

表格

<form  method="post">
<div id="ordernumbers">
          <select name ="designerorder_id">
            <option>Select Order</option>
          </select>
        </div>
<input type="text" name="paid_status" />

<input name="register1" type="submit"  onclick="return updatepayment(); "/>

PHP

if (isset($_POST['register1'])) {

  $designerorder_id = trim($_POST['designerorder_id']);
  $paid_status = password_hash($_POST['paid_status'], PASSWORD_DEFAULT);

  $stmt = $reg_user->runQuery("SELECT * FROM order_details");
  $row = $stmt->fetch(PDO::FETCH_ASSOC);
  $stmt->execute();


    if ($reg_user->register1($designerorder_id,  $paid_status)) {
      $id = $reg_user->lasdID();
      $key = base64_encode($id);
      $id = $key;

      $message = " updated database";

   //   $subject = "Confirm updation";

    //  $reg_user->send_mail($email, $message, $subject);
      $msg = "database updated ";

    } else {
      echo "sorry , Query could no execute...";
    }
  }

脚本

function getOrderDetail(e)
  {
    var designerId=e.target.options[e.target.selectedIndex].value;    
    var url="http://sbdev2.kidsdial.com:81/php/site6/designerpaidstatus.php?designer_id="+designerId+"&opration=2";
       var request = jQuery.ajax( {
                url: url ,
                type: 'POST',                      
            } );

            request.done( function (result)
            {  
              document.getElementById('ordernumbers').innerHTML =result;

            } );
            request.fail( function ( error )
            {
                console.dir(error);             
            } );
     }

我尝试的是,

当我研究谷歌时,我发现像 link1 link2link3 这样的链接只使用了Select [options] [不是输入类型] 在表单内。在其他一些links 他们正在使用 JS 来实现这一点,我也尝试过link5 并尝试了以下代码:

$selected_val = $_POST['designerorder_id'];
$stud = explode("_",$_POST['designerorder_id']);

但没有什么对我有用。我几乎花了 2 天的时间才提出问题,我对 php 完全陌生,请帮助我。

编辑 - posting.php文件

的完整代码
<?php 

require_once 'dbconfig.php';
require_once 'class.user.php';
include 'home.php';
print_r($_POST);
//$reg_user = new USER();
$reg_user='';


$stmt = $user_home->runQuery("SELECT * FROM tbl_users");
  $stmt->execute(array(":uid" => $_SESSION['userSession']));
  $row = $stmt->fetch(PDO::FETCH_ASSOC);
  $stmt->execute();
?>

<?php

var_dump($_POST);

  if (isset($_POST['register1'])) {



  $designerorder_id = trim($_POST['designerorder_id']);
//  $selected_val = $_POST['designerorder_id'];
//$stud = explode("_",$_POST['designerorder_id']);
  $product_id = trim($_POST['product_id']);
  $paid_status = password_hash($_POST['paid_status'], PASSWORD_DEFAULT);
  $due_date = trim($_POST['due_date']);

  $stmt = $reg_user->runQuery("SELECT * FROM order_details");
  $row = $stmt->fetch(PDO::FETCH_ASSOC);
  $stmt->execute();


    if ($reg_user->register1($designerorder_id, $product_id, $paid_status, $due_date)) {
      $id = $reg_user->lasdID();
      $key = base64_encode($id);
      $id = $key;

      $message = " updated database";

   //   $subject = "Confirm updation";

    //  $reg_user->send_mail($email, $message, $subject);
      $msg = "database updated ";

    } else {
      echo "sorry , Query could no execute...";
    }
  }



?>

<style type="text/css">
  .paymentstatus{width:70%;margin: auto!important;}
  .inputpayment{/*width: 20%;clear:both;*/}
</style>


<div class="paymentstatus">
 <form class="form-signin" method="post"  action="posting.php" id="myFormId"> 
    <h2 class="form-signin-heading">Designer Payment Status</h2> 


    <table>


    <!-- Designer -->
      <tr>

        <td> Designer  </td>

        <td>
          <select onchange="getOrderDetail(event);" >
            <option>Select Designer</option>
            <?php while($data = $stmt->fetch())  { if($data['type']=="admin")continue;?>
              <option value="<?php echo $data['userID'];?>"><?php echo $data['name'];?></option>
            <?php } ?>
          </select> 
        </td> 

      </tr> 


     <!-- Designer Order Number: -->

    <tr>

        <td>Number: </td>

      <td>

        <div id="ordernumbers">
          <select name ="designerorder_id">
            <option>Select Order</option>
            <option value="1">abc</option>
          </select>
        </div>

      </td>

    </tr> 


      <!-- Product Related To Order Number:  -->

      <tr>  

        <td>Products</td>

        <td>

        <div id="productnumbers">
          <select name="product_id">
            <option>Select Products</option>
          </select>
          </div>

        </td>

      </tr>


      <!--  Paid Status: -->

      <tr>
        <td>Paid :</td>


        <td>
          <input type="text" class="inputpayment" placeholder="PaidStatus" name="paid_status" value="Paid" readonly="readonly" />
        </td>

      </tr>


      <!--  DueDate:-->

       <tr>
        <td>Due Date</td>


        <td>
          <input type="text" class="inputpayment" id="datepicker" placeholder="Duedate" name="due_date" value=""/>
        </td>
      </tr>



    </table>
    <hr/>


   <input class="btn btn-large btn-primary" name="register1" type="submit" id="btnSubmit" value="Update Payment" onclick="return updatepayment(); "/>

  </form>

</div>
<link rel="stylesheet" href="http://sbdev2.kidsdial.com:81/php/site6/bootstrap/css/jquery-ui.css"> 

<script src="http://sbdev2.kidsdial.com:81/php/site6/bootstrap/js/outthinking/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(

  /* This is the function that will get executed after the DOM is fully loaded */
  function () {
    $( "#datepicker" ).datepicker({
      changeMonth: true,//this option for allowing user to select month
      changeYear: true //this option for allowing user to select from year range
    });
  }

);
  function getOrderDetail(e)
  {
    var designerId=e.target.options[e.target.selectedIndex].value;  
//var designerId=e.target.options[e.target.options.selectedIndex].‌​value;  
    var url="http://sbdev2.kidsdial.com:81/php/site6/designerpaidstatus.php?designer_id="+designerId+"&opration=2";
       var request = jQuery.ajax( {
                url: url ,
                type: 'POST',                      
            } );

            request.done( function (result)
            {  
              document.getElementById('ordernumbers').innerHTML =result;

            } );
            request.fail( function ( error )
            {
                console.dir(error);             
            } );
     }
  function getProductDetail(e)
  {    
    var productId = $("#dproductselect option:selected").attr("class");  
    var finalstrig=productId.split(",");
    alert(finalstrig[1]);
    var select='';
    select+='<select><option>Select Product</option>';

    for(i=0;i<finalstrig.length;i++)
    {
      if(finalstrig[i]!=0)
      {
        select +='<option value="'+finalstrig[i]+'">'+finalstrig[i]+'</option>';
      }
    }
    select +='</select>';   
    document.getElementById('productnumbers').innerHTML =select;
  }

</script>

<script>

var request = jQuery.ajax( {
                url: 'posting.php' ,
                data : jQuery("#myFormId").serialize(),
                type: 'POST',                      
            } );


</script>

class.user.php

public function register1($designerorder_id, $product_id, $paid_status, $due_date)
  {
    try {

      $stmt = $this->conn->prepare("INSERT INTO order_details(designerorder_id, product_id, paid_status, due_date) 
            VALUES(:designerorder_id, :product_id, :paid_status, :due_date) ;");
      $stmt->execute(array(
          ":designerorder_id" => $designerorder_id,
          ":product_id" => $product_id,
          ":paid_status" => $paid_status,
          ":due_date" => $due_date

      ));
      return $stmt;
    } catch (PDOException $ex) {
      echo $ex->getMessage();
    }
  }

【问题讨论】:

  • 使用isset()方法检查designerorder_id是否设置
  • @Karthi 当我尝试代码时:if (isset($_POST["designerorder_id"]) &amp;&amp; !empty($_POST["designerorder_id"])) { echo "Yes, mail is set"; }else{ echo "N0, mail is not set"; },我得到了这个结果N0, mail is not setarray(0) { }
  • 是的,这就是问题所在。
  • @Karthi 请告诉我如何设置designerorder_id,我在整个谷歌中研究过,都提到如果只有selected options,他们没有提到如果两者都有@ 987654340@......
  • @Karthi 以单一形式,我包括两种类型的值 - 一种是 input type' : `,另一种是 &lt;select name ="designerorder_id"&gt;,用户将在输入字段中输入值并选择值,所以我想将这两个值都保存在数据库中......但是当我提交表单时,我收到错误:Notice: Undefined index: designerorder_id

标签: javascript php html forms


【解决方案1】:

首先&lt;select name= "designerorder_id"&gt;去掉name属性后面的空格,应该是&lt;select name="designerorder_id"&gt;

然后像这样为表单提供一些 id。

<form  method="post" id="myFormId">

并在 ajax 表单中像这样传递表单数据。

var request = jQuery.ajax( {
                url: url ,
                data : jQuery("#myFormId").serialize(),
                type: 'POST',                      
            } );

正如我所见,您正在使用 post 方法在 ajax 中发送数据,那么为什么要在您的 url 字符串中使用 get 变量?

使用&lt;input type="hidden"&gt; 传递您其他需要的变量及其值

我猜这将是简单的方法。

您的函数function getProductDetail(e) 还需要进行一些更改 你写了

select+='<select><option>Select Product</option>';

改成select+='&lt;select name="product_id"&gt;&lt;option&gt;Select Product&lt;/option&gt;';

删除var request = jQuery.ajax( { url: 'posting.php' , data : jQuery("#myFormId").serialize(), type: 'POST',
} );
,如果您正在刷新页面,则不需要这个。

在您的 php 代码中将 $paid_status = password_hash($_POST['paid_status'], PASSWORD_DEFAULT); 更改为 $paid_status = $_POST['paid_status'];

希望这会有所帮助。

【讨论】:

  • 谢谢,我通过更改url: 'posting.php' , 尝试了您的代码,但我仍然遇到同样的错误,我需要更改其他内容吗?
  • Notice: Undefined index: designerorder_id in line $designerorder_id = trim($_POST['designerorder_id']);
  • 你的服务器端代码是什么?打印所有 POST 变量并检查值。使用print_r($_POST) 检查你得到的变量和值,
  • Array ( [paid_status] =&gt; Paid [due_date] =&gt; 11/06/2016 [register1] =&gt; Update Payment ) 我得到了这个结果.....
【解决方案2】:

我在phpfiddle.org 中尝试过,效果很好。

代码:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="ordernumbers">
          <select name ="designerorder_id">
            <option>Select Order</option>
            <option value="One">One</option>  
          </select>
        </div>
<input type="text" name="paid_status" />

<input name="register1" type="submit"  onclick="return updatepayment(); "/> <br><br>

<?php
if (isset($_POST['register1'])) {

  $designerorder_id = trim($_POST['designerorder_id']);
     echo $designerorder_id;  
}
?>

输出:

【讨论】:

  • 我不确定您的库文件。我已经尽力了。谢谢
  • 谢谢,我在这里删除了名称属性后的空格:&lt;select name ="designerorder_id"&gt;,现在解决了,现在我收到错误:Notice: Undefined index: product_id
  • @RohitJindal ,他在 name 属性后用 select 得到了空格,这就是问题所在,
  • @PunitGajjar 在我的回答中,有空格,但没有造成任何问题。所以,空白不是问题。
  • @abcd 在 html 中没有名称为 product_id 的元素。这就是它给你这个错误的原因。
【解决方案3】:

能否请您更改您的表单并尝试

<form  method="post">
    <div id="ordernumbers">
              <select name ="designerorder_id">
                 <option value="">Select Order</option>
                <option value="1">abc</option>
              </select>
            </div>
    <input type="text" name="paid_status" />

    <input name="register1" type="submit"  onclick="return updatepayment(); "/>

【讨论】:

  • 谢谢,我试过那个代码,仍然得到同样的错误:Notice: Undefined index: designerorder_id in line $designerorder_id = trim($_POST['designerorder_id']);
  • 你的表格和他的有什么区别?停止给出不完整的答案,@abcd 你为什么奖励一个糟糕的答案?
  • @abcd - 你能添加一个或多个选项值并尝试吗?
  • @Masivuye Cokile - 我尝试了这段代码,但没有收到任何“错误”,如果我的答案不完整,我很抱歉。
  • @Shanukk 我尝试通过添加此代码:&lt;option value="1"&gt;abc&lt;/option&gt;,仍然是同样的错误....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 2012-04-05
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多