在 PHP 代码中使用 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21020667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:51:08  来源:igfitidea点击:

Using JavaScript inside PHP code

javascriptphpmysqldatabase

提问by niven300

Ok so I have a booking system that I am working on and I have events created in a table which gets populated by PHP if the admin adds new events. I would like to run a script if the user tries to book an event and then submit the form to php. I would like to do this because I want to know if the user has a voucher and wants to pay either by card or via paypal.

好的,所以我有一个我正在使用的预订系统,并且我在一个表中创建了事件,如果管理员添加新事件,该表会由 PHP 填充。如果用户尝试预订活动然后将表单提交给 php,我想运行一个脚本。我想这样做是因为我想知道用户是否有优惠券并想通过卡或通过贝宝支付。

Here is the table screenshot, sorry only a link I need 10 rep points.

这是表格截图,抱歉只有一个链接,我需要 10 个代表点。

http://thomasniven.com/table.jpg

http://thomasniven.com/table.jpg

The PHP code that creates the tables:

创建表的 PHP 代码:

<?php
$con = mysql_connect("localhost","root","");

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

mysql_select_db("flexiflash", $con);
 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>";

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

while($row = mysql_fetch_array($result))
{
    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' name='submit' value='Book now'></td>";
    echo "</tr>";
    echo "</form>";
    echo "</tbody>";
}

mysql_close($con);
?>

IF YOU NEED ANY MORE INFO LET ME KNOW

如果您需要更多信息,请告诉我

采纳答案by Ben

PHP is backend. JavaScript is frontend. What do you want to do in the frontend? Perform the check() function? If so, just put this

PHP是后端。JavaScript 是前端。你想在前端做什么?执行 check() 函数?如果是这样,只要把这个

<?php

echo '<script>
function check() {
  //do something
  return true;
}
</script>';

?>

before the table. This should do it. But it's kinda ugly :)

桌子前。这应该做。但它有点丑:)