dos71253 2013-08-13 06:03
浏览 106
已采纳

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

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

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.

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.

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:

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

HTML button to add a presentation:

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

Both Javascript functions:

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:

<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:

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.

Is there a better way to approach this?

  • 写回答

3条回答 默认 最新

  • drhzc64482 2013-08-13 06:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?