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.
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
You can't create a php variable via html as php is server side and html is client side. :)
Related
What I have:
I have a form with two normal text boxes and two disabled text boxes.
The disabled elements receive their values from the enabled elements via jQuery.
The values of the four elements are then posted via PHP
Note: My form is being used as part of acustom WordPress page.
The problem:
The disabled elements (i.e. values derived via jQuery) are not posting via PHP.
Visual:
My code:
PHP:
<?php
if(isset($_POST['submit'])) {
echo 'a= '.$_POST["textbox_a"].'<br />';
echo 'b= '.$_POST["textbox_b"].'<br />';
echo 'c= '.$_POST["textbox_c"].'<br />';
echo 'd= '.$_POST["textbox_d"].'<br />';
}
?>
jQuery:
<script type="text/javascript">
jQuery(document).ready(function($) {
// Code here will be executed on document ready. Use $ as normal.
// alert("jQuery is working fine :-)");
$( "button" ).click(function( event ) {
event.preventDefault();
$('[name=textbox_c]').val($('[name=textbox_a]').val());
$('[name=textbox_d]').val($('[name=textbox_b]').val());
});
});
</script>
HTML:
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="input" name="textbox_a" />
<input type="input" name="textbox_b" />
<button>[jQuery] Click me to transfer</button><br />
<input type="input" name="textbox_c" disabled="disabled" />
<input type="input" name="textbox_d" disabled="disabled" />
<input type="submit" name="submit" value="[PHP] Click me to post" /><br />
</form>
My question:
Why isn't PHP treating my jQuery derived values as regularly inputted values? Though I've never used Ajax, am I correct in assuming that Ajax is the solution? The examples I've seen for Ajax seem a little overkill for what I'm trying to achieve but I'm happy to delve into it if it's the only way forward.
use readonly instead of disabled
From Mozilla:
Disabled
This Boolean attribute indicates that the form control is not available for interaction. In particular, the click event will not be dispatched on disabled controls. Also, a disabled control's value isn't submitted with the form.
Disabled inputs will not be submitted with the form; that's part of the defined behaviour of disabled.
The disabled elements (i.e. values derived via jQuery) are not posting via PHP.
Disabled elements do not get sent to the server. You can use readonly instead of disabled, the difference is readonly inputs are sent to the server.
I don't believe disabled form elements actually ever get posted (regardless of being dynamically created by jQuery). I would use the readonly="readonly" attribute instead.
For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.
I want to submit a form to the database and I want to use a sprite image instead of regular submit buttons..
Here is the images I'm using
<div class="cancel">
</div>
<div class="save_and_new">
</div>
<div class="save_and_quit">
</div>
if(isset(......)){
}
I have no idea what to put in the isset function ...
Do i need to set names to the images? or what?
You could just use
<input type="image src="/your/button/image/here.gif" />
instead of the images nested inside anchors.
The only problem would be that you can't directly sense which button exactly was pressed because <input type="image" /> does not post a value. If you really need multiple post buttons that also post a value:
<button name="button" value="action1"><img src="/your/image/here.gif" alt="action 1" /></button>
<button name="button" value="action2"><img src="/your/image/here.gif" alt="action 2" /></button>
You can do it in Jquery. Try this,
$("#save_and_new_btn").click(function() {
$("#form").submit();
});
#form is id of form
Generally, a form is submitted when the user presses a submit button. However, sometimes, you may need to submit the form programmatically using JavaScript.
JavaScript provides the form object that contains the submit() method. Use the ‘id’ of the form to get the form object.
For example, if the name of your form is ‘myform’, the JavaScript code for the submit call is:
document.forms["myform"].submit();
But, how to identify a form? Give an id attribute in the form tag
<form id='myform' action='formmail.pl'>
Here is the code to submit a form when a hyperlink is clicked:
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
Search
</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>
Source: How to Submit a Form Using JavaScript
You need to use javascript.
Find a form which has to be submitted. Then add actions to each elements. Whene they're clicked you are submitting (or canceling) form.
i have two submit button on my index page namely International and Domestic. i want that two different button to point to different pages namely int.php and dom.php when i click on the buttons. can you help me out. thank
while it is allowed only to define single action = "" for form element. but if i have to do that, i would do it this way.
<form action ="somepage.php" method="post">
<!--all your input elements-->
<input type="submit" name ="international" value="international"/>
<input type="submit" name ="domestic" value="domestic"/>
</form>
determine which button have been clicked and act accordingly.
if(isset($_POST['domestic']) {
//include dom.php
}
else if(isset($_POST['international']) {
//include int.php
}
and then you can include the necessary file.
or the other way is to go with AJAX/jQuery way
you can just use switch in php for differ or
use javascript
Do it with jquery! First, dont create submit buttons just create
<input type="button" />
Than give them an id like:
<input type="button" id="InternationalBTN" />
<input type="button" id="DomesticBTN" />
and with jquery bind the action
$(document).ready(function(){
$("#InternationalBTN").bind('click',function(){
$('#idOfYourForm').attr('action','setTheDestinationPageHere');
document.forms['nameOfYourForm'].submit();
});
});
That will not be possible since your form's action attribute can only point to one location at a time and both buttons are in the same form(but possible if you use AJAX).
If you wanted to use pure PHP (i.e. no Javascript involved), you'd have to write two different handlers for the different button clicks, like below:
if(isset($_POST["name_of_international_button"])){
//actions to perform for this --
}
if(isset($_POST["name_of_domestic_button"])){
//action to perform for this
}
In the actions part of each of the handlers, you could then do a redirect, with the URL containing the data to be processed either in the int.php or dom.php scripts
You can do it in this way:
In form tag please leave empty action action=""
2 buttons to send:
<input class="btnSend" type="submit" name="International" value="International" id="International"/>
<input class="btnSend" type="submit" name="Domestic" value="Domestic" id="Domestic"/>
and use ajax:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function(){
$('#International ').click(function() {
// send to file 1 using ajax
});
$('#Domestic').click(function() {
// send to file 2 using ajax
});
});
</script>
Here is how to send data using ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Your form action would have to contain some sort of conditional statement to redirect users based on which submit button is clicked
<form action="<?php if(isset($_POST['international'])){echo 'international.php';}else{echo 'domestic.php';}?>" method="post">
<input type="text" name="field1" />
<input type="text" name="field2"/>
<input type="submit" value="international "name="international"/>
<input type="submit" value="domestic "name="domestic"/>
</form>
Or you could set up your conditionals on a page specified by the form actionand have them redirect based on which button was clicked,
Just put a form tag, and set the action to the page. Then the submit button will navigate to that page where the action tag is pointing to...
Easy as that :D
In PHP, i want to open more that one window and each one of them need to recieve different post data.
i know how to redirect a page via header location + Get DATA, but i really need to be able to send POST data.
EDIT:
Graph
|-Page with post 1
Main --|-Page with post 2
|-Page with post 3
so basically 1 page goes and open 3 pages with different post data on each. must be done server Side.
why not use three forms? ;-) Code tested, works.
<form method='post' action='http://<sever>/post1.php' id='action_frm1' name="action_frm1" target="_blank">
<input type='hidden' name='param1' value='hello'/>
<input type='hidden' name='param2' value='world'/>
</form>
<form method='post' action='http://<sever>/post2.php' id='action_frm2' name="action_frm2" target="_blank">
<input type='hidden' name='param1' value='hello2'/>
<input type='hidden' name='param2' value='world2'/>
</form>
<form method='post' action='http://<sever>/post3.php' id='action_frm3' name="action_frm3" target="_blank">
<input type='hidden' name='param1' value='hello3'/>
<input type='hidden' name='param2' value='world3'/>
</form>
<script type='text/javascript'>
function makePostRedirect() {
document.getElementById('action_frm1').submit();
document.getElementById('action_frm2').submit();
document.getElementById('action_frm3').submit();
}
makePostRedirect()
</script>
Of course, if you want post data via serverside usefull link will be this: http://noobflash.com/server-side-post-with-php/
you may use the following trick:
<form method='post' action='action.php' id='action_frm'>
<input type='hidden' name='param1' value='hello'/>
<input type='hidden' name='param2' value='world'/>
</form>
<script type='text/javascript'>
function makePostRedirect() {
document.getElementById('action_frm').submit();
}
</script>
You will need to do a workaround by calling window.open to some stub PHP pages that can then use some JavaScript to call a form post. What your asking can not really be done in PHP because PHP is server side and not front end. Your looking for a much more JavaScript reliant solution.
You can do a single POST to a new window by putting a target="_new" on the <form> element. It won't validate, but it will open ONE new window and submit the form data via that window.
For multiple windows, you'll have to hack together some JS to open the multiple windows, insert a form with a copy of the data you want to have posted in that window, then trigger the posts individually.
how about this:
Original page ---opens---> page 1 ---opens---> page 2 ---opens---> page 3
Each page passes the data for the remaining pages. Search for javascript pop-up code that works and runs on page open.