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

如何通过单击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?

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.

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

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