简体   繁体   English

如何从另一个PHP页面创建和引入变量

[英]How would I create and bring in a variable from another PHP page

I have a table that is created in php. 我有一个在php中创建的表。 I have called this page events. 我称此为页面事件。 I have used an include function within my user page to display the table so that a user can use the events to book an event. 我在用户页面中使用了include函数来显示表,以便用户可以使用事件来预订事件。 The thing is, I now need to create a page that will run once a user has clicked on the book now button. 关键是,我现在需要创建一个页面,该页面将在用户单击“立即预订”按钮后立即运行。 This page will update the booking table in my database. 该页面将更新我数据库中的预订表。 I am completely lost here and don't know how to go about creating this page as I think I need a variable that is brought in from the events table, perhaps via the button. 我在这里完全迷失了方向,不知道如何创建该页面,因为我认为我需要从事件表(可能是通过按钮)中引入的变量。 The booking table in the database has these columns. 数据库中的预订表具有这些列。 Booking_ID, User_ID, Event_ID, Payment_Type and Date_Booked. 预订ID,用户ID,事件ID,付款类型和预订日期。 I know I am using mysql but I will get the newer versions after I have this all working. 我知道我使用的是mysql,但在所有这些工作之后,我将获得更新的版本。 I just don't know where to start on the creation of the booking PHP and inserting the values from the events table into the database. 我只是不知道从哪里开始创建预订PHP并将事件表中的值插入数据库。 Any help will be greatly appreciated. 任何帮助将不胜感激。

This is the events table that is displayed on the user page to book an event 这是在用户页面上显示的用于预订事件的事件表

<?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);
?>

If you need any more code or any other information please feel free to ask. 如果您需要更多代码或任何其他信息,请随时询问。

I know my code is not great and I am using Javascript inside my PHP but I am still learning PHP. 我知道我的代码不是很好,我在PHP中使用Javascript,但仍在学习PHP。 Only been doing this for about 2 months, so I know its not the best. 只做了大约2个月,所以我知道这不是最好的。

If the page you are sending your data to is on the same host then SESSION might be what youre looking for. 如果您要将数据发送到的页面在同一主机上,那么您可能正在寻找SESSION。 You can also use the form to POST data, via a form action to another page to process the data. 您还可以使用表单来发布数据,方法是通过表单操作将表单发布到另一个页面来处理数据。 If the data isnt sensitive then you can pass vars between pages with GET or even cookies, but it seems that passing the data isn't your main concern here. 如果数据不敏感,则可以在具有GET甚至Cookie的页面之间传递var,但是似乎传递数据并不是您主要关心的问题。

One option to look at is creating a separate page to handle the form processing and insert the data, this can be called in the form action tag 要查看的一个选项是创建一个单独的页面来处理表单处理并插入数据,这可以在表单操作标签中调用

It sounds like you are new to this and it might over complicate things but it is worth looking at MVC to separate concerns . 听起来您是一个新手,可能会使事情复杂化,但是值得一看的是MVC来分离问题

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

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