PHP 从另一个页面获取值
PHP get values from another page
我有这个页面 admin_inquiry.php(page1),它有一个动态 table 显示 records.I 的行想要从 2 列 ContactNo 和消息中获取值。 contactno 列包含 link,它转到 admin_sms.php(第 2 页)并在文本字段中显示联系人号码。
第 1 页:
<td><a href="admin_sms.php?ContactNo=<?php echo $row_ContactUs['ContactNo']; ?>">Send SMS</a></td>
第 2 页:
<input name='number' type='text' id="number" value="<?php if(isset($_GET['ContactNo'])){echo $_GET['ContactNo'];}else{echo "";}?>">
我还想从第 1 页获取列消息的内容并将其显示在第 2 页的文本区域中。但它应该显示属于特定 id 或其他内容的消息。我听说过会议,但我还不太明白。你能告诉我怎么做吗?
更新
我在 admin_inquiry.php
中试过了
$_SESSION['message'] = $row_ContactUs['message'];
admin_sms.php
$_SESSION['message'];
<textarea name="frmMsg" id="frmMsg" cols="45" rows="5"><?php echo $_SESSION['message'];?></textarea>
问题是,它显示相同的消息。不是属于某个id或something.Help我的对应消息。
方法一:
使用POST
方法提交表单。这样,您可以根据您为 <td>
元素提供的名称或 ID 查询与联系号码相关的 Message 元素。 (你还没有给过)。
方法二:
动态设置联系人编号元素的 href 属性,使用 jQuery
。将操作属性设置为 URL,其中包括 ContactNo 和 Message 作为 URL parameters
。这样你也可以从 GET
数组访问消息,就像你使用联系号码一样。(注意:不确定像消息这样的长文本是否可以作为 URL 参数传递,取决于长度,并且可能是安全问题)。
如果您仍然无法解决这个问题,请在您的 table 中添加一个 html form
更新包括分页
第一个解决方案,创建一个文件并调用它contact.php
<?php
$db = new PDO('mysql:host=localhost; dbname=data','root','');
$contacts = $db->query('SELECT * FROM contactdetails');
//creating pagination
$nav_counter = basename($_SERVER['SCRIPT_FILENAME']); //getting name of the current page
$current_page = $nav_counter;
$nav_counter = rtrim($nav_counter, ".php"); //getting the name of the current page and removing the extension
//creating pagination pages
$next_page = $nav_counter + 1;
$prev_page = $nav_counter - 1;
//getting row count, we are going to use it to limit our query and to create pagination
$row_count = $contacts->rowCount();
//number of records to show per page
$num_records = 5;
//getting the last page
$last_page = ($row_count / $num_records) - 1;
if(!is_int($last_page)){
$last_page = (int)$last_page + 1;
}
//displaying records
$start = 0;
$limit = $num_records; //number of records to show
if ($current_page !== 'admin_inquiry.php'){
$start = ($limit * $nav_counter);
}
//getting number of rows left in the table
$rows_left = $db->query("SELECT * FROM contactdetails ORDER BY ID limit $start,$limit");
$num_rows = $rows_left->rowCount();
//if records left in the table is less than the number of records to show $num_records
if ($num_rows < $num_records) {
$limit = $num_rows; //limit is equal to the number of records left in the table
}
//getting number of records from the table
$contacts = $db->query("SELECT * FROM contactdetails ORDER BY ID limit $start,$limit");
//displaying pagination and creating pages if they don't exists
$pages = array();
for ($counter = 1; $counter <= $last_page; $counter++) {
$pages[] = $counter;
}
//storing pages in array and creating a page if it doesn't exist
foreach ($pages as $num) {
$page = $num.'.php';
if(file_exists($page)== false && $num <= $last_page){
copy('admin_inquiry.php', $page);
}
}
?>
创建一个 pagination.php 文件
<p class="pagenav"><a href="<?php if($current_page == '1.php'){ echo 'admin_inquiry.php';}else{echo $prev_page.'.php';} ?>" <?php if($current_page == 'admin_inquiry.php'){echo 'class="hide"';} ?>><</a> <a href="<?php echo $next_page.'.php'; ?>" <?php if($next_page > $last_page){echo 'class="hide"';} ?>>></a></p>
创建一个 css 文件 admin.css
table {
width: 100%;
margin: 0 auto;
}
table, td, th {
border: 1px solid black;
}
th {
height: 50px;
}
#div{
width: 40%;
margin: 0 auto;
}
.pagenav a{
display: inline;
width: 100px;
padding: 10px;
height: 100px;
border-radius: 50px;
font-size: 20px;
color: #fff;
line-height: 50px;
text-align: center;
text-decoration: none;
background-color: #0186ba;
}
.pagenav a:hover{
background-color: #fff;
border: 1px solid #000;
color: #000;
}
.hide {
display: none !important;
}
p{
text-align: center;
}
admin_iquiry.php 文件
<?php
require_once('contact.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="admin.css">
</head>
<body>
<div id="div">
<table>
<tr>
<th>ID</th>
<th>Contact No.</th>
<th>Message</th>
</tr>
<?php foreach ($contacts as $value) : ?>
<tr><td><?php echo $value['ID']; ?></td><td><?php echo $value['ContactNO']; ?></td><td><?php echo $value['Message'] ?></td><td><form action="admin_sms.php" method="post"><input type="hidden" name="ContactNo" value="<?php echo $value['ContactNO']; ?>"><input type="hidden" name="Message" value="<?php echo $value['Message']; ?>"><input type="Submit" value="Send SMS" name="send_sms" /></form></td></tr>
<?php endforeach; ?>
</table>
<?php require_once('pagination.php'); ?>
</div>
</body>
</html>
admin_sms.php
<?php
if(isset($_POST['send_sms'])){
$contactNo = $_POST['ContactNo'];
$message = $_POST['Message'];
}
?>
html代码
<div style="margin:0 auto; width:50%">
<p>Message for: <?php echo $contactNo; ?></p>
<textarea><?php echo "$message"; ?></textarea>
第二个解决方案使用 $_GET
<?php
require_once('contact.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="admin.css">
</head>
<body>
<div id="div">
<table>
<tr>
<th>ID</th>
<th>Contact No.</th>
<th>Message</th>
</tr>
foreach ($contacts as $value) : ?>
<tr><td><?php echo $value['ID']; ?></td>
<td><?php echo $value['ContactNO']; ?></td>
<td><?php echo $value['Message'] ?></td>
//use id of the contact no and in the next page use that id to query data related to that id
<td><a href="admin_sms.php?id=<?php echo $value['ID']; ?>">Send Sms</a></td></tr>
<?php endforeach; ?>
</table>
<?php require_once('pagination.php'); ?>
</div>
</body>
</html>
admin_sms.php
<?php
$db = new PDO('mysql:host=localhost; dbname=data','root','');
if(isset($_GET['id'])){
$id= $_GET['id'];
$results = $db->query("SELECT * FROM contactdetails WHERE ID = $id");
foreach ($results as $value) {
$contactNo = $value['ContactNO'];
$message = $value['Message'];
}
}
?>
在你的 html
<div style="margin:0 auto; width:50%">
<p>Message for: <?php echo $contactNo; ?></p>
<textarea><?php echo "$message"; ?></textarea>
我有这个页面 admin_inquiry.php(page1),它有一个动态 table 显示 records.I 的行想要从 2 列 ContactNo 和消息中获取值。 contactno 列包含 link,它转到 admin_sms.php(第 2 页)并在文本字段中显示联系人号码。
第 1 页:
<td><a href="admin_sms.php?ContactNo=<?php echo $row_ContactUs['ContactNo']; ?>">Send SMS</a></td>
第 2 页:
<input name='number' type='text' id="number" value="<?php if(isset($_GET['ContactNo'])){echo $_GET['ContactNo'];}else{echo "";}?>">
我还想从第 1 页获取列消息的内容并将其显示在第 2 页的文本区域中。但它应该显示属于特定 id 或其他内容的消息。我听说过会议,但我还不太明白。你能告诉我怎么做吗?
更新 我在 admin_inquiry.php
中试过了$_SESSION['message'] = $row_ContactUs['message'];
admin_sms.php
$_SESSION['message'];
<textarea name="frmMsg" id="frmMsg" cols="45" rows="5"><?php echo $_SESSION['message'];?></textarea>
问题是,它显示相同的消息。不是属于某个id或something.Help我的对应消息。
方法一:
使用POST
方法提交表单。这样,您可以根据您为 <td>
元素提供的名称或 ID 查询与联系号码相关的 Message 元素。 (你还没有给过)。
方法二:
动态设置联系人编号元素的 href 属性,使用 jQuery
。将操作属性设置为 URL,其中包括 ContactNo 和 Message 作为 URL parameters
。这样你也可以从 GET
数组访问消息,就像你使用联系号码一样。(注意:不确定像消息这样的长文本是否可以作为 URL 参数传递,取决于长度,并且可能是安全问题)。
如果您仍然无法解决这个问题,请在您的 table 中添加一个 html form
更新包括分页
第一个解决方案,创建一个文件并调用它contact.php
<?php
$db = new PDO('mysql:host=localhost; dbname=data','root','');
$contacts = $db->query('SELECT * FROM contactdetails');
//creating pagination
$nav_counter = basename($_SERVER['SCRIPT_FILENAME']); //getting name of the current page
$current_page = $nav_counter;
$nav_counter = rtrim($nav_counter, ".php"); //getting the name of the current page and removing the extension
//creating pagination pages
$next_page = $nav_counter + 1;
$prev_page = $nav_counter - 1;
//getting row count, we are going to use it to limit our query and to create pagination
$row_count = $contacts->rowCount();
//number of records to show per page
$num_records = 5;
//getting the last page
$last_page = ($row_count / $num_records) - 1;
if(!is_int($last_page)){
$last_page = (int)$last_page + 1;
}
//displaying records
$start = 0;
$limit = $num_records; //number of records to show
if ($current_page !== 'admin_inquiry.php'){
$start = ($limit * $nav_counter);
}
//getting number of rows left in the table
$rows_left = $db->query("SELECT * FROM contactdetails ORDER BY ID limit $start,$limit");
$num_rows = $rows_left->rowCount();
//if records left in the table is less than the number of records to show $num_records
if ($num_rows < $num_records) {
$limit = $num_rows; //limit is equal to the number of records left in the table
}
//getting number of records from the table
$contacts = $db->query("SELECT * FROM contactdetails ORDER BY ID limit $start,$limit");
//displaying pagination and creating pages if they don't exists
$pages = array();
for ($counter = 1; $counter <= $last_page; $counter++) {
$pages[] = $counter;
}
//storing pages in array and creating a page if it doesn't exist
foreach ($pages as $num) {
$page = $num.'.php';
if(file_exists($page)== false && $num <= $last_page){
copy('admin_inquiry.php', $page);
}
}
?>
创建一个 pagination.php 文件
<p class="pagenav"><a href="<?php if($current_page == '1.php'){ echo 'admin_inquiry.php';}else{echo $prev_page.'.php';} ?>" <?php if($current_page == 'admin_inquiry.php'){echo 'class="hide"';} ?>><</a> <a href="<?php echo $next_page.'.php'; ?>" <?php if($next_page > $last_page){echo 'class="hide"';} ?>>></a></p>
创建一个 css 文件 admin.css
table {
width: 100%;
margin: 0 auto;
}
table, td, th {
border: 1px solid black;
}
th {
height: 50px;
}
#div{
width: 40%;
margin: 0 auto;
}
.pagenav a{
display: inline;
width: 100px;
padding: 10px;
height: 100px;
border-radius: 50px;
font-size: 20px;
color: #fff;
line-height: 50px;
text-align: center;
text-decoration: none;
background-color: #0186ba;
}
.pagenav a:hover{
background-color: #fff;
border: 1px solid #000;
color: #000;
}
.hide {
display: none !important;
}
p{
text-align: center;
}
admin_iquiry.php 文件
<?php
require_once('contact.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="admin.css">
</head>
<body>
<div id="div">
<table>
<tr>
<th>ID</th>
<th>Contact No.</th>
<th>Message</th>
</tr>
<?php foreach ($contacts as $value) : ?>
<tr><td><?php echo $value['ID']; ?></td><td><?php echo $value['ContactNO']; ?></td><td><?php echo $value['Message'] ?></td><td><form action="admin_sms.php" method="post"><input type="hidden" name="ContactNo" value="<?php echo $value['ContactNO']; ?>"><input type="hidden" name="Message" value="<?php echo $value['Message']; ?>"><input type="Submit" value="Send SMS" name="send_sms" /></form></td></tr>
<?php endforeach; ?>
</table>
<?php require_once('pagination.php'); ?>
</div>
</body>
</html>
admin_sms.php
<?php
if(isset($_POST['send_sms'])){
$contactNo = $_POST['ContactNo'];
$message = $_POST['Message'];
}
?>
html代码
<div style="margin:0 auto; width:50%">
<p>Message for: <?php echo $contactNo; ?></p>
<textarea><?php echo "$message"; ?></textarea>
第二个解决方案使用 $_GET
<?php
require_once('contact.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="admin.css">
</head>
<body>
<div id="div">
<table>
<tr>
<th>ID</th>
<th>Contact No.</th>
<th>Message</th>
</tr>
foreach ($contacts as $value) : ?>
<tr><td><?php echo $value['ID']; ?></td>
<td><?php echo $value['ContactNO']; ?></td>
<td><?php echo $value['Message'] ?></td>
//use id of the contact no and in the next page use that id to query data related to that id
<td><a href="admin_sms.php?id=<?php echo $value['ID']; ?>">Send Sms</a></td></tr>
<?php endforeach; ?>
</table>
<?php require_once('pagination.php'); ?>
</div>
</body>
</html>
admin_sms.php
<?php
$db = new PDO('mysql:host=localhost; dbname=data','root','');
if(isset($_GET['id'])){
$id= $_GET['id'];
$results = $db->query("SELECT * FROM contactdetails WHERE ID = $id");
foreach ($results as $value) {
$contactNo = $value['ContactNO'];
$message = $value['Message'];
}
}
?>
在你的 html
<div style="margin:0 auto; width:50%">
<p>Message for: <?php echo $contactNo; ?></p>
<textarea><?php echo "$message"; ?></textarea>