使用javascript / ajax

时间:2015-04-24 06:20:04

标签: php jquery html mysql ajax

我必须通过ajax提交一些form字段,like multiple checkboxes selection和一些hidden input fields,并将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内容。提交后我参加了我想要回复的文件。

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

尽管如此,你仍可以实现它。

0x100000000

<强>的Ajax

jQuery(document).ready(function($){
     $("#your_button_id").on('click', function(e){
         e.preventDefault();
         $("#your_form_id") .submit();
     })
})

答案 1 :(得分:0)

你可以像这样在你的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/