Button in side a Form - php

<form action='login.php' method='post'>
<center> Username : <input type='text' name='username'> Password : <input type='password' name='pass'> <input type='submit' value='Login'></center>
</form>
<input type='submit' value='Registration'>
I wondering if it is possible to have my Registration button next to the login without having anything to do with the login.php. If I move it inside of the form then it's going to the login.php and not the reg.php.

Do not use submit button in anchor(<a></a>) tag
Just provide a text in it
Registration
If you want a button in it
Try to use type button not submit (Untested from me)
<input type='button' value='Registration'>

If you can - you may consider changing the form action to some "formHandler.php", giving both of the submits the same name, and testing the inputs value (each one has to be different) in the formHanlder.php
$real_action=$_REQUEST['submit_input_name'];
switch ($real_action){
case 'register':.....
case 'login':.......
}

Related

Pass Value between pages in Php

I want to pass the value from this page to next page pay.php file below is my form (index.php )
<form action=\"/modules/gateways/pay.php\" method=\"post\" id=\"checkout[id]\">
<input type=\"hidden\" name=\"user\" value=\"[userid]\">
<input type=\"hidden\" name=\"decription\" value=\"[itemname]\">
<input type=\"hidden\" name=\"amount\" id=\"amount[id]\" value=\"[price]\">
<input type=\"hidden\" name=\"type\" value=\"deposit\">
</form>
and in pay.php, I am trying to get the value with this code:
$m_amount = number_format($_POST['amount'], 2, ".", "");
$m_desc = strip_tags($_POST['decription']);
I am not getting values in pay.php.
You need to check your HTML.
<form action="/modules/gateways/pay.php" method="post" id="checkout[id]">
<input type="hidden" name="user" value="123">
<input type="hidden" name="decription" value="itemone">
<input type="hidden" name="amount" id="amount_id" value="538">
<input type="hidden" name="type" value="deposit">
</form>
and in pay.php try to receive to simply print POST variable
print_r($_POST);
Include JQuery or a similar JS framework or use native JS to submit this form.
Without a submit-button you have to submit this form by JS or a JS-framework.
In JQuery you have to write something similar to this example:
$('#checkout[FORMID]').submit();
This will submit the form. For sure you have to complete this but basicly this is the missing call.
I saw your form contains hidden inputs. If this form depends on another form/action or else you should either include the hidden form in the depoending form or submit it by itself using JS/JQuery/something similar.

Why do I have two GET variables with same name in url

When I click submit button of form I get two variables with same name in url. Why this happens.
The form is following
<select name='name1' form='select_form'>...</select>
<select name='name2' form='select_form'>...</select>
<form id='select_form' action='index.php' method='get'>
<input type='text' name='date_start' value='val1' id='datepicker1'>
<input type='text' name='date_end' value='val2' id='datepicker2'>
<button id='submit' type='submit' value='Submit'>Select</button>
</form>
When I click submit button I can see the following url
index.php?name1=val_name1&name2=val_name2&date_start=2016-08-01+00%3A00%3A00&date_start=&date_end=2016-08-03+00%3A00%3A00&date_end=2016-08-03+00%3A00%3A00
As you can see there are two date_start variables. What is the reason?
This has very bad impact, because when I change only one value (for example only date_start) then after clicking submit I have the following
index.php?name1=val_name1&name2=val_name2&date_start=2016-08-01+00%3A00%3A00&date_start=&date_end=2016-08-03+00%3A00%3A00&date_end=
So second value of date_end is empty.

Set different targets for form with multiple submit buttons

I have a form that contains a WYSIWYG editor, and two submit buttons :
<input type='submit' name='pdf' value='PDF Preview'/>
<input type='submit' name='save' value='Save'/>
"pdf" action displays the content of the editor as a PDF output. "save" action is a regular form submit. I want the PDF output to open in a new tab, and can't figure how to do that.
There is no "target" attribute for "input" tag. I could add a "target=_blank" to the "form" tag, but that would submit my "save" action in a new tab as well, which I don't want.
I tried to replace the "pdf" submit button with this :
<a href="same_page" target="_blank" onclick="submitForm();">
That didn't work. The form is submitted in its current tab and the new tab query receives nothing in $_POST.
Is there a magic trick I don't know yet ?
Note : server-side code is PHP
Use button and onclick event with jQuery.
<button type='submit' name='pdf' onclick="$('form').attr('target', '_blank');">PDF Preview</button>
<button type='submit' name='save' onclick="$('form').attr('target', '');">SAVE</button>
So in short you have one form + two submit buttons
first button: open in same tab
second button : open in new tab
after submit you want to know which one is submitted
Solution:
add two submit buttons with different behavior is impossible
so you need JS help or Jquery ( Ravi Hirani solution is perfect )
To know which button is submitted, you should give different names ( newage comment is perfect)
So this is just simple example does the magic you are looking for:
<?php
//print the post data
var_dump($_POST);
?>
<script type="application/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<form action="test.php" method="POST">
<!-- just simple textarea -->
<textarea rows="4" cols="50" name="textarea">
text here
</textarea>
<!-- new tab submit button -->
<input type="submit" name="New_Window" value="New Window" onclick="$('form').attr('target', '_blank');" />
<!-- same tab submit button -->
<input type="submit" name="Same_Window" value="Same Window" onclick="$('form').attr('target', '');" />
</form>
You could maybe try using jQuery for this.
For example when the PDFPreview button is clicked jQuery could save the values of the form inputs into cookies. Then it would redirect to the PDFPreview page in a new tab. The PDFPreview page would then read the cookies.
E.g:
<script>
var input1 = null;
function previewPdf()
{
input1 = $("#input1").text();
document.cookie = "input1=" + input1;
window.open("/path/to/pdfPreview.php", '_blank')
}
</script>
<form action="whateverpage" method="post">
<input type="text" name="input1" id="input1">
<input type="button" onClick="previewPdf();" value="Preview PDF">
<input type="submit" value="Save">
</form>
You can use HTML5 formtarget attribute of input tag to change form behavior. This won't work with outdated browsers, however. In your case it will look something like:
<input type='submit' name='pdf' formtarget="_blank" value='PDF Preview'/>
<input type='submit' name='save' formtarget="_self" value='Save'/>
It can be used with button tag either.
References:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/submit#formtarget
https://www.w3schools.com/tags/att_input_formtarget.asp
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Attributes

call a php function or file from a HTML input button

What is the best way to implement php code to run when
a button is pressed/clicked. Done some research but couldn't find anything relevant apart of few examples that show use of JavaScript.
I want it to be purely in PHP.
At this precise moment I have created the buttons in the following way <input type='button' value='Click Me'/> now not sure what to do. Could anyone help please.
I don't think you can run a PHP script by native HTML only. Using JavaScript would be the simplest solution you can do.
The basics of a form and php. when the button is pushed, the form calls an action (some page, even itself). Then the php checks if the button was pushed. If so, do something, if not, ignore it. Instead of creating a button though, a submit would work better, but the button works just fine.
<form method="post" action="./PHPScripts.php" />
<input type='button' value='Click Me' name='button' /> <-- added a name
<input type='submit' value='Click Me' name='submitButton' />
<input type='submit' value='Click Me' name='submitButton2' />
</form>
// below would be on the page called by the action in the above form
// which can be on the same page with no problem, but larger amounts of
// code become more difficult to read, so having multiple files is recommended
<?php
if($_POST['button']) {
// php code (either execute it, or call function)
}
if($_POST['submitButton']) {
// php code (either execute it, or call function)
}
if($_POST['submitButton2']) {
// php code (either execute it, or call function)
}
?>
It's best to keep php code on top of html if on same page, but for demonstration reasons, I've put it on the bottom. (Easier to read)
The idea is, you will need to give the button the name attribute. Depending on how the form, is then submitted, in this case, to itself or to another page determined by the form action attribute and the method, the script will then run.
<form action="submit.php" method="post">
<input type='button' value='Click Me' **name="submit"**/>
</form>
if(isset($_POST['submit']) {
//runs when the button is clicked
}
<form action="index.php" method="post">
<input type='button' name='btn_func' value='Click Me'/>
</form>
<?php
if(isset($_POST['btn_func'])){
//call the function here.
}
?>
If you have 10 buttons:
<form action="index.php" method="post">
<input type='button' name='btn_func_1' value='Click Me 1'/>
<input type='button' name='btn_func_2' value='Click Me 2'/>
<input type='button' name='btn_func_3' value='Click Me 3'/>
<input type='button' name='btn_func_4' value='Click Me 4'/>
<input type='button' name='btn_func_5' value='Click Me 5'/>
<input type='button' name='btn_func_6' value='Click Me 6'/>
<input type='button' name='btn_func_7' value='Click Me 7'/>
<input type='button' name='btn_func_8' value='Click Me 8'/>
<input type='button' name='btn_func_9' value='Click Me 9'/>
<input type='button' name='btn_func_10' value='Click Me 10'/>
</form>
<?php
if(isset($_POST['btn_func_1'])){
//call the function here.
}
?>
Or use a Conditional/Switch statement.
If you are going to use input type="button", I am assuming you will need JavaScript to handle the click event? Then make an Ajax request to a PHP file or something?
Or, you could change your input type from "button" to "submit" <input type="submit" name="submitButton" id="submitButton" value="Submit" />
Then the...easiest (for lack of better words) is to check at the top of your page for the submit post variable. If it's there, PHP will handle it. Or, it will display the form if the variable is not set.
<?php
if (isset($_POST['submitButton']))
{
// form has been submitted.
// do something with PHP.
}
?>
<form action="file.php" method="post">
...
<input type="submit" name="submitButton" id="submitButton" value="Submit" />
</form>

How can i submit inputs located inside the <div style="display:none">?

The case :
<form id='frm' name ='frm' action='test.php'>
<div style='display:none'>
<input type='text' name='name' value ='' />
</div>
<input type='submit' value='Submit' />
</form>
For example , How can i submit the from given above with its inputs ?
Issue : The "name" input wont be passed !
Programatically, you can just trigger the submit event on the form element:
$('#frm').submit();
Edit: Actually, after reading your markup more carefully, you are using an input named name, this element can cause "clashing" problems with the form's name attribute.
Consider changing the name of your input.
See also:
Unsafe Names for HTML Form Controls
As a user, you'd just click the submit button. The visibility of a form element doesn't change the fact that it gets submitted with the form.
Programmatically:
document.getElementById('frm').submit();
If you're not trying to show the input, why not use type="hidden" and dispense with the style?
document.getElementById('frm').submit();
Here is how to submit hidden variables in a FORM:
<input type='hidden' name='name' value ='' />

Categories