php - 使用 javascript/ajax 提交具有不同输入类型的 php 表单

标签 php jquery html mysql ajax

我必须提交form对于某些字段,like multiple checkboxes selection和一些hidden input fields通过ajax并用响应替换html内容。最后我选择了 javascript/ajax...但我错在哪里?

 <?php include( 'session.php');
 $userid=$_SESSION[ 'Userid'];
 include( 'connection.php');
 ?>

    <head>
     <script>
 function myFunction() {
var soi = document.getElementById("sweaterownerid").value;
var osp = document.getElementById("osweaterpic").value;
var osi = document.getElementById("osweaterid").value;
var value = [];
        $("input[name*='" + sweater+ "']").each(function () {
       // Get all checked checboxes in an array
        if (jQuery(this).is(":checked")) {
         value.push($(this).val());
            }
        });


var dataString = 'soi1=' + soi + '&osp1=' + osp  + '&osi1=' + osi + '&value1=' + value;
if (soi1 == '' || osp1 == '' || osi1 == '' || value1 == '') {
alert("Please Fill All Fields");
} else {
// AJAX code to submit form.
$.ajax({
type: "POST",
url: "Usercloset1.php",
data: dataString,
cache: false,
success: function(response) {
$('#mydiv').replaceWith(response);
}
});
}
return false;
}
</script>
    </head>
   <div id="mydiv">
    <div class="padding-top">
        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 ">
            <div class="shop_item" style="width:100%;">


                <form id="myForm">
                    <?php
                    $sweaterid=$_GET['d'];
                    $sownerid=$_GET['e'];
                    $opic=$_GET['f'];             

                    $query1="select * from `usersweater` where `Sweaterid`='$sweaterid'";
                    $result1=mysql_query($query1);
                    $row1=mysql_fetch_assoc($result1);
                    $sweaternikname=$row1['SNickname'];

                    ?>

                    <div>
                        <ul class="sweaters">
                            <li> <h4><?php echo $sweaternikname; ?></h4> <img src="upload/<?php echo $opic; ?>"> </li>
                        </ul>
                        <ul class="sweater1">

                            <?php
        $query="select * from `usersweater` where `Userid`='$userid' && `Swap`='0' ";
                   $result = mysql_query($query);


            while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
                                $sid = $line[Sweaterid];
                                $img = $line[Sweaterpic];
                                $nikname = $line[SNickname];
                                $size = $line[Size];
                            ?>

<li> <h4><?php echo $nikname; ?><input type="checkbox" name="sweater[]" value="<?php echo $sid; ?>" /></h4> <img src="upload/<?php echo $img; ?>"> </li>

<?php  } ?>
                        </ul>
                    </div>


                    <input type="hidden" name="sweaterownerid" value="<?php echo $sownerid; ?>">
                    <input type="hidden" name="osweaterpic" value="<?php echo $opic; ?>">
                    <input type="hidden" name="osweaterid" value="<?php echo $sweaterid; ?>">

                    <input type="submit" name="next" onclick="myFunction()" value="NEXT" class="btn woo_btn btn-primary" style="margin-left: 30px;">
                    <input type="button" name="cancel" value="CANCEL" class="btn woo_btn btn-primary">
                </form>
            </div>
        </div>

        <div class="clearfix"></div>
                    <hr>
                </div>
    </div>

我想将所选选项传递到另一个页面,我现在使用表单操作来执行此操作。但我希望它是动态的,而不需要重新加载页面。我是 ajax/javascript 新手。

第二件事是,我如何处理响应,在提交此表单时我想用我们使用 ajax 获得的响应替换第一页内容。这意味着用其他页面的 html 内容替换所有 html 内容。我在提交后附上了我想要回复的文件。

<div class="padding-top">
	<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 ">
		<div class="shop_item" style="width:100%;">

				<div style="text-align:center;">
					<h4>Are you sure you want to swap?</h4>
				</div>

			<form action="Usercloset2.php" method="post">
				<?php
				include('session.php');
				include('connection.php');
				
				
				
				foreach ($_POST['value1'] as $sid){
			$query1="select * from `usersweater` where `Sweaterid`='$sid'";
				$result1=mysql_query($query1);
				$row1=mysql_fetch_assoc($result1);
				$sweaternikname=$row1['SNickname'];
				$sweaterpic=$row1['Sweaterpic'];
				?>

				<div style=" ">
					<ul class="sweaters">
						<li> <h4><?php echo $sweaternikname; ?></h4> <img src="upload/<?php echo $sweaterpic; ?>"> </li>
					</ul>
				</div>


				<!-------requester's own sweater details--------------->
				<input type="hidden" name="sid[]" value="<?php echo $sid;?>">
				<input type="hidden" name="snikname[]" value="<?php echo $sweaternikname;?>">
				<input type="hidden" name="spic[]" value="<?php echo $sweaterpic;?>">
				
				<?php } ?>

				<!-------requester's show intrest that sweater details--------------->
				<?php
				$sownerid=$_POST['soi1'];
				$opic=$_POST['osp1'];
				$sweaterid=$_POST['osi1'];
				?>
				<input type="hidden" name="sweaterownerid" value="<?php echo  $sownerid;?>">
				<input type="hidden" name="osweaterpic" value="<?php echo  $opic;?>">
				<input type="hidden" name="osweaterid" value="<?php echo  $sweaterid;?>">
				
				<div style="float:right; margin-right:10px;">
					<input type="submit" name="next" value="NEXT" class="btn woo_btn btn-primary">
					<input type="button" name="cancel" value="CANCEL" class="btn woo_btn btn-primary">
				</div>
			</form>
		</div>
	</div>

	<div class="clearfix"></div>
		<hr>
	</div>

最佳答案

你可以像这样在 Ajax 中使用:

 var form = $('#your_form_id');
 var formAction = $('#your_form_id').attr('action');

 /* Get input values from form */
 values = form.serializeArray();

 /* Because serializeArray() ignores unset checkboxes and radio buttons: */
 values = values.concat(
   $('#your_form_id input[type=checkbox]:not(:checked)').map(
     function() {
       return {
         "name": this.name,
         "value": this.value
       }
     }).get()
 );

 $.ajax({
   'ajax code here'
 });

或者您可以查看https://api.jquery.com/serialize/

关于php - 使用 javascript/ajax 提交具有不同输入类型的 php 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840072/

相关文章:

php - PHP错误:mysqli_num_rows()期望参数1为mysqli_result,boolean给出[duplicate]

jquery - 本地托管站点的文件路径

javascript - 如何叠加两个GetCanvas?

javascript - 当我删除溢出 :hidden property from my navbar it disappears

php - PHP的处理顺序是什么?

php - 如何使用选择查询的结果更新一行的多个 mysql 列?

php - gammu发送短信返回错误29

javascript - JQuery 滚动共享图标

php - 如何从 MySQL 数组中检索选中的复选框?

javascript - Jquery 仅在移动设备上操作 DOM