如何从jquery返回的值可以用作php变量

时间:2015-02-26 04:48:01

标签: php jquery



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

请检查&#34; display.php&#34;中的代码。显示[行号..]

完整代码位于 display.php 中。我还附上了页面的屏幕截图&#34; output.jpg&#34;,我遇到了问题。

这是我想要完成的事情:

首先,用户将从下拉列表中选择项目。然后在所选项目的基础上,表-1被填满。 然后,当用户点击第一个表的任何行时,将选择订单号,从而在第二个表中显示订单的其他详细信息。

我在第一张桌子上没有任何问题。当用户单击第一个表的一行时出现问题。订单编号确实被选中[第18行到第33行],并在文本框中使用语法&#34; $(&#39; #order&#39;)。val(响应)&#34; [行号。 31]。

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

但是当我尝试使用$ _POST [&#39; txtorder&#39;]检索文本框的值时,没有显示任何内容。为什么[请检查行号。 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 个答案:

答案 0 :(得分:-1)

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

1)PHP在服务器端而非客户端运行

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

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

使用ID

在PHP中创建容器
<?php echo "<div id='container'>placeholder</div>"; ?>

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

使用ID

在PHP中创建容器
$("#container").val("My new value");

或者您可以使用此但不推荐使用

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