如何将jquery返回的值用作php变量


how value returned from jquery can be used as a php variable

			var a;
			function getdatafromddl(){
				a = document.getElementById("payflag").value;
				location.href="?a="+a;
			}
			$(document).ready(function() {
				var selectedRow;
				var record;
				
				$('tr').click(function () {
					$('tr').removeClass('selected');
					$(this).addClass('selected');
					selectedRow = $(this);
				});
				$('#viewbtn').click(function() {
					var td = $(selectedRow).children('td');
					//alert(td[0].innerText);
					var value = td[0].innerText;
					//$('#order').val(value); 
					
					$.post('pickorderno.php',{value:value}, function(response){
						$('#order').val(response); 
						//alert(response);
					});
				});
			});
.centr-div { margin:0px auto; background-color:#fff; width:92%; height:500px}
.f-col{ float:left; width:50%; height:36px; margin-top:14px; background-color: #fff }
.s-col{ float:left; width:25%; height:40px; margin-top:10px; background-color: #fff }
.l-col{ float:left; width:25%; height:40px; margin-top:10px; background-color: #fff }
.spc{ float:left; width:100%; height:15px; background-color: #F4F4F4 }
#statdd { float:right; font-family:verdana; font-size:10pt; }
.pay { width:60%; height:20pt; font-family:verdana; font-size:10pt; }
#show-recs{ border-collapse: collapse; }
table { width: 98%; }
th{ width:20%; height: 12px; font-family:Arial; font-size: 12pt; border-bottom: 1px solid #ECEBEB; color: #960001 }
td { border-bottom: 1px solid #ECEBEB; }
tr { height: 25px; text-align: center; color: #0000BD }
.selected { background-color: #80FFFF; }
.showViewbtn{ width:25%; height: 25px; color:#0000FF; text-decoration:none; background-color:#fff; font-family:verdana; font-size:10pt }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
			<div align="center">
				<img src="images/menu.jpg" alt="" />
			</div>
			<div class="centr-div">
				<div class="f-col"> <label id="statdd">Show orders on basis of&nbsp;&nbsp;:&nbsp;&nbsp;</div>
				<div class="s-col">
					<select id="payflag" onchange="getdatafromddl()" class="pay">
						<option value = "-1">---- Selection ----</option>
						<option value = "Paid" <?php if($_GET['a'] == 'Paid') echo "selected"; ?> >Paid</option>
						<option value = "Un-Paid" <?php if($_GET['a'] == 'Un-Paid') echo "selected"; ?> >Un-Paid</option>
					</select>
				</div>
				<div class="l-col">
					<input type="text" id="order" name="txtorder" value="" />
				</div>
		<!-- *******   Table - 1   ***********  -->
				<?php
					if (isset($_GET['a'])) {
					  $val = $_GET['a'];
					}
					if(!empty($val)){
						if ($val == 'Un-Paid')
						{
							$getrecs = "SELECT u.t_ordid, DATE_FORMAT(u.tord_dt,'%d-%m-%Y') as label_date, c.u_name, s.stat_desc  FROM utmp_orders as u join user_dtls as c on c.user_id=u.user_id join status as s on s.stat_cd=u.stat_cd WHERE u.pay_flag='N'";
							$retrv = mysqli_query($connection, $getrecs);
							}else if ($val == 'Paid') {
							$getrecs = "SELECT u.t_ordid, DATE_FORMAT(u.tord_dt,'%d-%m-%Y') as label_date, c.u_name, s.stat_desc  FROM utmp_orders as u join user_dtls as c on c.user_id=u.user_id join status as s on s.stat_cd=u.stat_cd WHERE u.pay_flag='Y'";
							$retrv = mysqli_query($connection, $getrecs);
						}
						else{ $retrv="";}
					}
				?>
				<table id='show-recs'>
					<thead>
						<tr>
							<th>Order No.</th>
							<th>Order Dt.</th>
							<th>Client Name</th>
							<th>Status</th>
						</tr>
					</thead>
						<?php 
							if (!empty($retrv)){
							while($retrvarr = mysqli_fetch_assoc($retrv)){ 
						?>
							<tbody>	
								<tr>
									<td><?php echo $retrvarr["t_ordid"] ?></td>
									<td><?php echo $retrvarr["label_date"] ?></td>
									<td><?php echo $retrvarr["u_name"] ?></td>
									<td><?php echo $retrvarr["stat_desc"] ?></td>
								</tr>
							</tbody>
						<?php } ?>
				</table>
				<div class="spc"></div>	
				<div align="center"><input type="submit" id="viewbtn" class="showViewbtn" value="View Selected Record" /></div>
				<div class="spc"></div>
		<!-- *****   Table - 2   *************  -->
				<?php
					//$ono=null;
					$ono = isset($_POST['txtorder']) ? $_POST['txtorder'] : null;
					echo 'VAL :'.$ono;	
				
					//$ono = 2;
					if(!empty($ono)){
						$selrec_query = "SELECT u.file_path, u.lbl_qty, u.lbl_prc, s.scat_name FROM utmp_orders as u join subcategory as s on s.scat_id = u.scat_id where u.t_ordid = {$ono}";
						$result = mysqli_query($connection, $selrec_query);
					}	
				?>
				<table id='show-sel-recs'>
						<thead>
							<tr>
								<th>Label Image</th>
								<th>Qty</th>
								<th>Price (&pound;)</th>
								<th>Category</th>
								<th>Payment Recd</th>
							</tr>
						</thead>
						<?php 
							if (!empty($result)){
							while($resultarr = mysqli_fetch_assoc($result)){ 
						?>
								<tbody>	
									<tr>
										<td><img src="<?php echo $resultarr['file_path'] ?>" alt="" style="width:61px; height:54px" /></td>
										<td><?php echo $resultarr["lbl_qty"] ?></td>
										<td><?php echo $resultarr["lbl_prc"] ?></td>
										<td><?php echo $resultarr["scat_name"] ?></td>
										<td></td>
									</tr>
								</tbody>
							<?php }} ?>
				</table>
			</div>

请检查[行号..] 中显示的"display.php"中的代码

完整的代码在display.php中。我还附上了页面"output.jpg"的屏幕截图,我遇到了问题。

以下是我想要实现的目标:

首先,用户将从下拉菜单中选择项目。然后根据所选项目,填写表1。然后,当用户点击第一个表的任何一行时,订单号被拾取,从而在第二个表中显示订单的其他细节。

我在第一张桌子上没有任何问题。当用户单击第一个表的一行时,问题就出现了。订单号do被提取[第18至33行],并使用语法"$('#Order').val(response)"[第31行]显示在文本框中。

我使用了一个文本框来将jquery变量的值存储在文本框中。完成后,下一步将根据文本框中的选定记录运行sql查询,该文本框将相应地填充第二个表。

但是,当我尝试使用$_POST['xtorder']检索文本框的值时,什么都没有显示。为什么【请检查101至105行】

display.php

    1.<?php include ("dbcon.php"); ?>
2.
3.<!DOCTYPE html>
4. <html>
5.  <head>
6.   <title></title>
7.   <link rel="stylesheet" type="text/css" href="workstyle.css">
8.   <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
9.   <script>
10. var a;
11. function getdatafromddl(){
12.           a = document.getElementById("payflag").value;
13.           location.href="?a="+a;
14. }
15.   </script>
16.
17.   <script>
18. $(document).ready(function() {
19.           var selectedRow;
20.           var record;
21.           $('tr').click(function () {
22.               $('tr').removeClass('selected');
23.        $(this).addClass('selected');
24.        selectedRow = $(this);
25.           });
26.
27.           $('#viewbtn').click(function() {
28.       var td = $(selectedRow).children('td');
29.       var value = td[0].innerText;
30.       $.post('pickorderno.php',{value:value}, function(response){
31.                $('#order').val(response); 
32.       });
33.           });
34. });
35.   </script>
36.  </head>
37.
38.  <body>
39.    <div align="center">
40.      <img src="images/menu.jpg" alt="" />
41.    </div>
42.    <div class="centr-div">
43.
44.    <div class="f-col"> <label id="statdd">Show orders on basis of&nbsp;&nbsp;:&nbsp;&nbsp;</div>
45.    <div class="s-col">
46.      <select id="payflag" onchange="getdatafromddl()" class="pay">
47.        <option value = "-1">---- Selection ----</option>
48.        <option value = "Paid" <?php if($_GET['a'] == 'Paid') echo "selected"; ?> >Paid</option>
49.        <option value = "Un-Paid" <?php if($_GET['a'] == 'Un-Paid') echo "selected"; ?> >Un-Paid</option>
50.      </select>
51.    </div>
52.    <div class="l-col"> <input type="text" id="order" name="txtorder" value="" /> </div>
53.
54.    <!-- ********   Table - 1   ********  -->
55.
56.    <?php
57. if (isset($_GET['a'])) {
58.   $val = $_GET['a'];
59. }
60. if(!empty($val)){
61.          if ($val == 'Un-Paid')
62.          {
63.          $getrecs = "SELECT u.t_ordid, DATE_FORMAT(u.tord_dt,'%d-%m-%Y') as label_date, c.u_name, s.stat_desc  FROM utmp_orders as u join user_dtls as c on c.user_id=u.user_id join status as s on s.stat_cd=u.stat_cd WHERE u.pay_flag='N'";
64.      $retrv = mysqli_query($connection, $getrecs);
65.   }else if ($val == 'Paid') {
66.      $getrecs = "SELECT u.t_ordid, DATE_FORMAT(u.tord_dt,'%d-%m-%Y') as label_date, c.u_name, s.stat_desc  FROM utmp_orders as u join user_dtls as c on c.user_id=u.user_id join status as s on s.stat_cd=u.stat_cd WHERE u.pay_flag='Y'";
67.      $retrv = mysqli_query($connection, $getrecs);
68.          }
69.        else{ $retrv="";}
70. }
71.    ?>
72.    <table id='show-recs'>
73. <thead>
74.   <tr>
75.     <th>Order No.</th>
76.     <th>Order Dt.</th>
77.     <th>Client Name</th>
78.     <th>Status</th>
79.          </tr>
80. </thead>
81.        <?php 
82. if (!empty($retrv)){
83.   while($retrvarr = mysqli_fetch_assoc($retrv)){ 
84.        ?>
85.     <tbody> 
86.          <tr>
87.     <td><?php echo $retrvarr["t_ordid"] ?></td>
88.     <td><?php echo $retrvarr["label_date"] ?></td>
89.     <td><?php echo $retrvarr["u_name"] ?></td>
90.     <td><?php echo $retrvarr["stat_desc"] ?></td>
91.          </tr>
92. </tbody>
93.        <?php }} ?>
94.    </table>
95.    <div class="spc"></div>  
96.    <div align="center"><input type="submit" id="viewbtn" class="showViewbtn" value="View Selected Record" /></div>
97.    <div class="spc"></div>
98.
99.<!-- *********   Table - 2   ************  -->
100.    <?php
101.    //$ono=null;
102.    $ono = isset($_POST['txtorder']) ? $_POST['txtorder'] : null;
103.    //$ono = $('#order').attr('value');
104.    
105.    echo 'VAL :'.$ono;  
106.
107.    //$ono = 2;
108.    if(!empty($ono)){
109.          $selrec_query = "SELECT u.file_path, u.lbl_qty, u.lbl_prc, s.scat_name FROM utmp_orders as u join subcategory as s on s.scat_id = u.scat_id where u.t_ordid = {$ono}";
110.          $result = mysqli_query($connection, $selrec_query);
111.    }   
112.    ?>
113.
114.    <table id='show-sel-recs'>
115.      <thead>
116.    <tr>
117.          <th>Label Image</th>
118.          <th>Qty</th>
119.          <th>Price (&pound;)</th>
120.          <th>Category</th>
121.          <th>Payment Recd</th>
122.    </tr>
123.      </thead>
124.      <?php 
125.        if (!empty($result)){
126.       while($resultarr = mysqli_fetch_assoc($result)){ 
127.      ?>
128.           <tbody>  
129.          <tr>
130.                <td><img src="<?php echo $resultarr['file_path'] ?>" alt="" style="width:61px; height:54px" /></td>
131.                <td><?php echo $resultarr["lbl_qty"] ?></td>
132.                <td><?php echo $resultarr["lbl_prc"] ?></td>
133.                <td><?php echo $resultarr["scat_name"] ?></td>
134.                <td></td>
135.          </tr>
136.           </tbody>
137.      <?php } ?>
138.    </table>
139.   </div>
140.  </body>
141.</html>
142.<?php //get_footer(); ?>

pickorderno.php

    <?php
    if(isset($_POST['value'])){
        $rethtml = $_POST['value'];
    }
    else {
        $rethtml = 'Invalid Data';
    }
    echo $rethtml;
?>

抱歉,图片没有上传[错误:发布图片至少需要10个信誉]

你的问题缺乏细节,但让我们试着给你一个正确的方法。

1) PHP运行在服务器端,而不是客户端

2) jQuery在客户端运行,而不是在服务器端上运行

因此。。。我建议您使用PHP创建容器(根据需要使用ID或类)。然后使用jQuery注入您的价值。

用PHP创建一个ID为的容器

<?php echo "<div id='container'>placeholder</div>"; ?>

?><div id='container'>placeholder</div><?php

用PHP创建一个ID为的容器

$("#container").val("My new value");

或者你可以使用这个,但不推荐

$("#container").val("<?php echo 'your value'; ?>")