【问题标题】:how to get the value of selected text box value using php如何使用php获取所选文本框值的值
【发布时间】:2015-08-27 11:51:07
【问题描述】:

我有属性列表,并且我得到了与我的选择标准匹配的用户列表。

我需要向选定的用户发送邮件。

我选中所有选项来检查每个用户。

我有文本框供每个用户更改租金值。

我选择了一些用户并点击发送,我需要在文本框中发送带有值的邮件。

我想在表单提交代码中获取选定的文本框值。

在上图中,每个属性都有一个复选框,checkbob 可以检查所有属性。我有价格文本框。

当我通过选择一些属性点击发送时,我想发送邮件发送已发布的租金值

这里是示例代码。

    while($tenant = mysqli_fetch_array($result))
        {
        echo '
        <tr>

            <td>
                <a href="property.php?id='.$pro['id'].'"><img src="'.(($pro['image_1']) ? $pro['image_1'] : 'images/placeholder.png').'" alt="" width="100"></a>
            </td>

            <td><a href="#">'.$name['name'].$tenant['tenant_id'].' </a><br> '.$pro['name'].'</td>

            <td><b>'.$tenant['lease_exp'].'</b><br><i>'.$pro['ready'].'</i></td>

            <td>'.$pro['type'].'</td>

            <td><div class="price"><strong>$</strong><span>'.$tenant['rent_from'].'-'.$tenant['rent_to'].'</span></div><br><br><input type="text" name="new_rent'.$i.'" value='.$pro['rent'].' class="form-control" style="width:30%;" ></td>

            <td>
                <input type="checkbox" name="rent[]" id="rent'.$i.'" value="'.$tenant['tenant_id'].'"> Send

            </td>
        </tr>
        ';
    }

    <script>
    $(document).ready(function() {
    $('#all').change(function(){
    var ckb_status = $("#all").prop('checked');
    $('input[type="checkbox"]').prop("checked", ckb_status );
    });
    });
    </script>

    if(isset($_POST['submit'])) {
      if(!empty($_POST['rent'])) {
          print_r($_POST);
        foreach($_POST['rent'] as $check) {

            $tenant_profile = "SELECT * FROM tenant where id=".$check;
            $prof = mysqli_query($link, $tenant_profile);
            $profile = mysqli_fetch_array($prof);



            //echo $tenant_profile;
            //echo "</br>";
            //echo $pro['name'];

            echo $_POST['new_rent'];echo "</br>";

            $to      = $profile['email'];
            $subject = 'Price Change Alert';
            $header = "From: ".ADMIN_EMAIL."\r\n"; 
            $header.= "MIME-Version: 1.0\r\n"; 
            $header.= "Content-Type: text/plain; charset=utf-8\r\n"; 
            $header.= "X-Priority: 1\r\n"; 
            $message = '' ;
            $message .= "<div><p>Dear User there is a price drop for the property
            <a href='".$_SERVER['SERVER_NAME']."/property.php?id=".$pro['id']."'>".$pro['name']."</a></p>
            <p>Owner offering you ".$_POST['new_rent'].". </p>
            <p>please contact the owner for the complete details. </p>
            <p>Owner email id:#</div>";

            $mail1 = mail($to, $subject, $message, $header);

            if($mail1) { echo 'mail sent successfully'; }


        }// foreach
      } //empty checkbox check
    else  { echo "Plase Check Any Result"; }
    }//submit

【问题讨论】:

  • 有什么问题?
  • 你说的文本框在哪里?
  • 他用文本框代替了复选框。 @AedixRhinedale
  • @AedixRhinedale 复选框用于选择房产,每个房产都有一个文本框来添加租金。我希望提交表单中的租金值
  • 在创建表格行的while 循环中,您在哪里增加$i?

标签: php jquery forms post checkbox


【解决方案1】:
Count how many checkbox are getting checked. 

$rent=$_POST['rent']; // Changes Done
$SelectedCheckbox=sizeof($rent); // Changes Done
for($i=0;$i<$SelectedCheckbox;$i++)
-----------------------------------------------------------------------
<?
while($tenant = mysqli_fetch_array($result))
        {?>
        <tr>
            <td>
                <a href="property.php?id='.$pro['id'].'">
                        <img src="images/placeholder.png" alt="" width="100">
                </a>
            </td>
            <td><a href="#"><?echo $name['name'].$tenant['tenant_id'];?></a><br><?echo $pro['name'];?></td>
            <td><b><?echo $tenant['lease_exp']?></b><br><i><?echo $pro['ready'];?></i></td>
            <td><?echo $pro['type'];?></td>
            <td>
                <div class="price"><strong>$</strong><span><?echo $tenant['rent_from'].'-'.$tenant['rent_to'];?></span></div><br><br>
                <input type="text" name="new_rent[]" value="<?echo $pro['rent']?>" class="form-control" style="width:30%;" ></td> //Changes Done
            <td>
                <input type="checkbox" name="rent[]" id="rent'.$i.'" value="<?echo $tenant['tenant_id'];?>"> Send
            </td>
        </tr>
        <?}>

    <script>
    $(document).ready(function() {
    $('#all').change(function(){
    var ckb_status = $("#all").prop('checked');
    $('input[type="checkbox"]').prop("checked", ckb_status );
    });
    });
    </script>

    if(isset($_POST['submit'])) 
    {
      if(!empty($_POST['rent'])) 
      {
        print_r($_POST);
        $rent=$_POST['rent']; // Changes Done
        $SelectedCheckbox=sizeof($rent); // Changes Done
        for($i=0;$i<$SelectedCheckbox;$i++) // Changes Done
        {
            $tenant_profile = "SELECT * FROM tenant where id=".$check;
            $prof = mysqli_query($link, $tenant_profile);
            $profile = mysqli_fetch_array($prof);

            echo $_POST['new_rent'];echo "</br>";

            $NewRent=$new_rent[$i]; //Changes Done

            $to      = $profile['email'];
            $subject = 'Price Change Alert';
            $header = "From: ".ADMIN_EMAIL."\r\n"; 
            $header.= "MIME-Version: 1.0\r\n"; 
            $header.= "Content-Type: text/plain; charset=utf-8\r\n"; 
            $header.= "X-Priority: 1\r\n"; 
            $message = '' ;
            $message .= "<div><p>Dear User there is a price drop for the property
            <a href='".$_SERVER['SERVER_NAME']."/property.php?id=".$pro['id']."'>".$pro['name']."</a></p>
            <p>Owner offering you ".$NewRent.". </p>
            <p>please contact the owner for the complete details. </p>
            <p>Owner email id:#</div>";

            $mail1 = mail($to, $subject, $message, $header);

            if($mail1) { echo 'mail sent successfully'; }


        }// for
      } //empty checkbox check
    else  { echo "Plase Check Any Result"; }
    }//submit

【讨论】:

  • 为什么他需要获取复选框的数量? foreach 自动执行此操作。
【解决方案2】:

$POST['new_rent'] 找不到任何内容,因为文本框被命名为 new_rent$i。

更好的方法是为复选框和文本框使用数组名称,并使用相同的$i 作为两者的数组索引。

$i = 0;
while($tenant = mysqli_fetch_array($result))
    {
    echo '
    <tr>

        <td>
            <a href="property.php?id='.$pro['id'].'"><img src="'.(($pro['image_1']) ? $pro['image_1'] : 'images/placeholder.png').'" alt="" width="100"></a>
        </td>

        <td><a href="#">'.$name['name'].$tenant['tenant_id'].' </a><br> '.$pro['name'].'</td>

        <td><b>'.$tenant['lease_exp'].'</b><br><i>'.$pro['ready'].'</i></td>

        <td>'.$pro['type'].'</td>

        <td><div class="price"><strong>$</strong><span>'.$tenant['rent_from'].'-'.$tenant['rent_to'].'</span></div><br><br><input type="text" name="new_rent['.$i.']" value='.$pro['rent'].' class="form-control" style="width:30%;" ></td>

        <td>
            <input type="checkbox" name="rent['.$i.']" id="rent'.$i.'" value="'.$tenant['tenant_id'].'"> Send

        </td>
    </tr>
    ';
    $i++;
}

然后就可以遍历数组,得到对应的输入了。

foreach ($_POST['rent'] as $i => $check) {
    $new_rent = $_POST['new_rent'][$i];
    $tenant_profile = "SELECT * FROM tenant where id=".$check;
    $prof = mysqli_query($link, $tenant_profile);
    $profile = mysqli_fetch_array($prof);



    //echo $tenant_profile;
    //echo "</br>";
    //echo $pro['name'];

    echo $_POST['new_rent'];echo "</br>";

    $to      = $profile['email'];
    $subject = 'Price Change Alert';
    $header = "From: ".ADMIN_EMAIL."\r\n"; 
    $header.= "MIME-Version: 1.0\r\n"; 
    $header.= "Content-Type: text/plain; charset=utf-8\r\n"; 
    $header.= "X-Priority: 1\r\n"; 
    $message = '' ;
    $message .= "<div><p>Dear User there is a price drop for the property
    <a href='".$_SERVER['SERVER_NAME']."/property.php?id=".$pro['id']."'>".$pro['name']."</a></p>
    <p>Owner offering you ".$new_rent.". </p>
    <p>please contact the owner for the complete details. </p>
    <p>Owner email id:#</div>";

    $mail1 = mail($to, $subject, $message, $header);

    if($mail1) { echo 'mail sent successfully'; }


}// foreach

【讨论】:

  • 感谢您的代码。但它不起作用。我得到单个值或第一个选定值,并且文本框值未提交到循环。
  • 我犯了一个错误。我将$i 放在字符串中,但没有注意到它是用单引号组成的,所以变量没有被扩展。我将其更改为使用串联。
猜你喜欢
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
相关资源
最近更新 更多