【问题标题】:How would I create and bring in a variable from another PHP page我将如何从另一个 PHP 页面创建和引入变量
【发布时间】:2018-06-05 01:56:13
【问题描述】:

我有一个创建于 php。我已将此页面称为事件。我在我的用户页面中使用了一个包含函数来显示表格,以便用户可以使用这些事件来预订一个事件。问题是,我现在需要创建一个页面,该页面将在用户单击“立即预订”按钮后运行。此页面将更新我数据库中的预订表。我在这里完全迷失了,不知道如何创建这个页面,因为我认为我需要一个从事件表中引入的变量,也许是通过按钮。数据库中的预订表有这些列。 Booking_ID、User_ID、Event_ID、Payment_Type 和 Date_Booked。我知道我正在使用 mysql,但是在我完成所有工作后我会得到更新的版本。我只是不知道从哪里开始创建预订 PHP 并将事件表中的值插入数据库。任何帮助将不胜感激。

这是在用户页面上显示的用于预订活动的活动表

<?php

$con = mysql_connect("localhost","root","");

if(!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("flexiflash", $con);

    // Creating the foundations for the table headings //
    echo "<thead>";
    echo "<tr>";
        echo "<th>ID</th>";
        echo "<th>Venue</th>";
        echo "<th>Event</th>";
        echo "<th>Date</th>";
        echo "<th>Time</th>";
        echo "<th></th>";
    echo "</tr>";
    echo "</thead>";

    // Running a JavaScript function to check for the user's payment type before submitting the form to book_event.php //
    echo '<script>
        function check() {

        if (document.getElementById("checkbox").checked && document.getElementById("card").checked)
        {
            alert("Pay by card with 20% off");
            return true;
        }
        else if (document.getElementById("checkbox").checked && document.getElementById("paypal").checked)
        {
            alert("Pay via PayPal with 20% off");
            return true;
        }
        else if (document.getElementById("card").checked)
        {
            alert("Pay by card without a voucher!");
            return true;
        }
        else if (document.getElementById("paypal").checked)
        {
            alert("Pay via PayPal without a voucher!");
            return true;
        }
        else 
        {
            alert("Nothing was checked. Please select your payment type");
            return false;
        }
    }
    </script>';

$result = mysql_query("SELECT * FROM events");

while($row = mysql_fetch_array($result))
{
    // Outputting the data from the $result variable into a formatted table //
    echo "<tbody>";
    echo "<form class='table-form'  action='book_event.php' method='post' onsubmit='return check()'>";
    echo "<tr>";
        echo "<td>" . $row['Event_ID'] . "</td>";
        echo "<td>" . $row['Event_Venue'] . "</td>";
        echo "<td>" . $row['Event_Name'] . "</td>";
        echo "<td>" . $row['Event_Date'] . "</td>";
        echo "<td>" . $row['Event_Time'] . "</td>";
        echo "<td><input type='submit' class='sub_btn'  value='Book now'></td>";
    echo "</tr>";
    echo "</form>";
    echo "</tbody>";
}

mysql_close($con);
?>

如果您需要更多代码或任何其他信息,请随时询问。

我知道我的代码不是很好,我在我的 PHP 中使用 Javascript,但我仍在学习 PHP。只做了大约 2 个月,所以我知道它不是最好的。

【问题讨论】:

  • 使用$_SESSION超级全局变量
  • 顺便说一句,不要使用这么多的 echo 语句。您可以关闭 php 标签 (?>) 并编写普通的 html,然后在需要时再次打开它。否则,一个带有换行符的 echo 语句将完美运行。
  • 我正在使用会话,只是不在此页面中,因为我仅将其包含在另一个页面中。

标签: php mysql html-table


【解决方案1】:

如果您将数据发送到的页面位于同一主机上,那么 SESSION 可能就是您要查找的内容。您还可以使用表单来 POST 数据,通过表单操作到另一个页面来处理数据。如果数据不敏感,那么您可以使用 GET 甚至 cookie 在页面之间传递变量,但似乎传递数据并不是您主要关心的问题。

查看的一个选项是创建一个单独的页面来处理表单处理和插入数据,这可以在the form action tag 中调用

听起来你是新手,这可能会使事情过于复杂,但值得关注MVC to separate concerns

【讨论】:

  • 感谢回复,我一定会看看的。
  • 我认为我需要的是这里的 Event_ID echo "" 。 $row['Event_ID'] 。 "";这样我就可以在预订页面上使用它来将表中的数据插入到我的数据库中。
猜你喜欢
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 2015-12-17
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多