繁体   English   中英

如何使用php回显从文本框表单发布的变量?

[英]How can i echo a variable posted from a textbox form using php?

更新:如果看到我的测试链接,您可以理解我的问题。 http://smstostudents.com/rmk/index3.php (选择任何名称并提交)

这是我的HTML表单代码

<input type="checkbox" name="student[]" value="1"><label>student name 1/label>
<input type="checkbox" name="student[]" value="2"><label>student name 2</label>
<input type="checkbox" name="student[]" value="3"><label>student name 3</label>

<textarea id="message" class="input" name="message" rows="7" cols="50">
Dear $studentname you have not paid the fees, please pay it as soon as possible.
</textarea><br />

php代码如下。

<?php
$message = $_POST['message'];
$servername = "localhost";
$dbusername = "xxxxxx";
$dbpassword = "xxxxxxx";
$dbname = "xxxxxxxx";
foreach($_POST['student'] as $value)
{
    // Create connection
    $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT firstname, email FROM Students WHERE id = $value"));
    $studentname = $getID['firstname'];
    $email = $getID['email'];
    echo $message;
}
?>

我要实现的是,在表单文本框中,“ $ studentname”将被数据库中学生的姓名替换。 但是echo $ message不显示学生姓名,而是显示,

“亲爱的$ studentname,您尚未支付费用,请尽快付款。”

如何用学生的名字代替“ $ studentname”。 抱歉,很简单,但是我被卡住了。 抱歉,我的英语不好,我是编码初学者。

更新:

实际上,我的表格没有问题。 该表格将值正确发布到php。 但是,文本框值包含“ $ studentname”,必须用取自数据库的学生姓名替换。 但是,PHP代码没有做到这一点,它只是在显示文本框内容。

提前致谢。

试试这个(您必须添加php来回显学生姓名标签)

<textarea id="message" class="input" name="message" rows="7" cols="50">
Dear <?php echo $studentname; ?> you have not paid the fees, please pay it as soon as possible.
</textarea><br /> 

尝试这个

<textarea id="message" class="input" name="message" rows="7" cols="50">
Dear 
<?php
echo $studentname ;
?>

you have not paid the fees, please pay it as soon as possible.
</textarea><br />

首先,您在一个foreach循环中连接到数据库...这很糟糕,会使脚本变慢。 尝试将连接移出循环。

其次,您要多次查询数据库以获取要从单个表中收集的数据。 有一种更简单的方法可以做到这一点。

这是一个例子:

<?php


$message = $_POST['message'];
$servername = "localhost";
$dbusername = "smstobdc_smstest";
$dbpassword = "removable";
$dbname = "smstobdc_smstest";

// Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//First we need to sanitize the values. or you will be open to a mysql injection...
$students = array_map('mysql_real_escape_string',$_POST['students']); // this sanitizes all values inside $_POST['students'];
//now let's prepare them for the query
$students = "'".implode("','",$students)."'"; //this formats it like so: '1','2','3','4'....
//now let's perform our query.. if you want to fetch all the results, then loop like this...
//Make sure you perform the query and save the result resource so you can catch an error like so...
$result = mysqli_query($conn, 'SELECT `firstname`, `email` FROM `Students` WHERE `id` IN ('.$students.')')
//The actual query looks like this:
//    SELECT `firstname`, `email` FROM `Students` WHERE `id` IN ('1','2','3','4','5','6'[,etc..])

//now we can iterate through the results for each student. This while loop will continue until all the data specified by your student id's is returned
while($getID = mysqli_fetch_assoc($result)) {

    $studentname = $getID['firstname'];
    $email = $getID['email'];   

    //NOTE: $studentname in $message is literally "$studentname". PHP will not treat this as a variable... what you can do is replace the text...
    echo str_replace('$studentname',$studentname,$message);
}
?>

如果您只想用快速而肮脏的方式对代码进行处理,那么这里也是如此:

<?php


$message = $_POST['message'];
$servername = "localhost";
$dbusername = "smstobdc_smstest";
$dbpassword = "removable";
$dbname = "smstobdc_smstest";



foreach($_POST['student'] as $value) {

// Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT firstname, email FROM Students WHERE id = $value"));
$studentname = $getID['firstname'];
$email = $getID['email'];   

    echo str_replace('$studentname',$studentname,$message);

}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM