javascript 如何通过单击 HTML 按钮更改 PHP 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18201760/
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 11:03:56  来源:igfitidea点击:

How to change a PHP variable with a HTML button click?

phpjavascripthtml

提问by Fizzix

I am trying to change a PHP variable with either a HTML button click, or another appropriate way.

我正在尝试通过单击 HTML 按钮或其他适当的方式来更改 PHP 变量。

Unfortunately, I am unable to post a majority of my code here, but I'll supply as much as possible.

不幸的是,我无法在这里发布我的大部分代码,但我会提供尽可能多的代码。

Firstly, I have two buttons on my site. They each call two different javascript functions, but they load the same HTML form. The form is then submitted to a MySQL database, depending on which button was clicked at the beginning.

首先,我的网站上有两个按钮。它们各自调用两个不同的 javascript 函数,但它们加载相同的 HTML 表单。然后将表单提交到 MySQL 数据库,具体取决于在开始时单击了哪个按钮。

Instead of having both the buttons load the same form, I would like them to load different forms. I would like to do this by saving a PHP variable when the user clicks on either button.

我希望它们加载不同的表单,而不是让两个按钮加载相同的表单。我想通过在用户单击任一按钮时保存 PHP 变量来实现此目的。

I have searched online quite a bit, but I found no direct answer to my question.

我在网上搜索了很多,但没有找到我的问题的直接答案。

Here is a slight example:

这是一个小例子:

HTML button to add a quiz:

添加测验的 HTML 按钮:

<h2>
<button id='opener' class='roundButton add' value='' onClick="OpenNewQuizDialog();">
</button>
Create Quiz
</h2>

HTML button to add a presentation:

添加演示文稿的 HTML 按钮:

<h2>
<button id='opener' class='roundButton add' value='' onClick="OpenNewPresDialog();
</button>
Add Presentation
</h2>

Both Javascript functions:

两个 Javascript 函数:

function OpenNewQuizDialog()
{
    $( "#dialog" ).dialog( "open" );
    $('#dialog').dialog({title:"Create Quiz"});
    $('#dialogtype').val("addquiz");
}

function OpenNewPresDialog()
{
    $( "#dialog" ).dialog( "open" );
    $('#dialog').dialog({title:"Create Presentation"});
    $('#dialogtype').val("addpres");
}

The HTML Form that is being opened:

正在打开的 HTML 表单:

<div id="dialog" title="Edit Settings">
        <form name="editForm" method="post" action="createplay.php">
        <input type=hidden id='dialogid' name='dialogid' value=''>
        <input type=hidden id='dialogtype' name='dialogtype' value=''>
        <input type=hidden id='uid' name='uid' value='<?php echo $userid; ?>'>
            <div>
            Name: <input type='text' id='nameEdit' name='nameEdit' value=''>
        </div>
        <div>
            Data: <textarea id='textEdit' name='textEdit' row='10' cols='50'></textarea>
        </div>
        <input type="submit" class="roundButton upload" value="" />
        </form>
    </div>

What I have tried:

我尝试过的:

I tried adding the following into the onClick part within the HTML buttons:

我尝试将以下内容添加到 HTML 按钮中的 onClick 部分:

quiz button: <?php $create_type = 'createquiz' ?>
presentation button: <?php $create_type = 'createpres' ?>

I then tried to make an 'if' statement to see which button was clicked, so I could show the appropriate form. Although, the value of $create_type was always 'createpres' since it was being called last on the page. It was like the PHP was being called, even though the HTML onClick was not being called.

然后我尝试做一个“if”语句来查看点击了哪个按钮,这样我就可以显示适当的表单。尽管 $create_type 的值始终是 'createpres',因为它是在页面上最后调用的。这就像调用了 PHP,即使没有调用 HTML onClick。

Is there a better way to approach this?

有没有更好的方法来解决这个问题?

回答by Jereme Causing

PHP is a server side language while Javascript runs in client. That means they both run separately. But if you would like them to process together, such as getting data to be displayed using javascript, you will have to use AJAX.

PHP 是一种服务器端语言,而 Javascript 在客户端运行。这意味着它们都单独运行。但是,如果您希望它们一起处理,例如使用 javascript 获取要显示的数据,则必须使用AJAX

回答by jeff

There is no need for the extra variables as you set a form control with the appropriate value

当您使用适当的值设置表单控件时,不需要额外的变量

$('#dialogtype').val("addquiz");

The form will receive this value:

表单将收到此值:

<input type=hidden id='dialogtype' name='dialogtype' value=''>

Now it is up to your php script (createplay.php) whether the $_POST['dialogtype'] == 'addquiz' ) or the other value and process the data as intended

现在取决于您的 php 脚本 (createplay.php) 是 $_POST['dialogtype'] == 'addquiz' ) 还是其他值并按预期​​处理数据

回答by shauns2007

You can't create a php variable via html as php is server side and html is client side. :)

您不能通过 html 创建 php 变量,因为 php 是服务器端而 html 是客户端。:)