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
Related
I made a html and a php page. I put 3 images in html page showing different genre of movies- horror, fantasy, romance. I want the images to work as form submit button and should get redirected to the php page and the php page should get the genre of the image.
What I already tried- I tried lot of different things but nothing worked.
And I wonder how php page will take different inputs from different images using $_POST method.
Expected output-
Suppose if user clicked on image of genre 'horror', then in php page value of $genre should be Horror.
You can use an <img> tag inside a <button> element.
<form method="post">
<button name="genre" value="horror" type="submit"><img src="./img/horror.jpg"></button>
<button name="genre" value="comedy" type="submit"><img src="./img/comedy.jpg"></button>
</form>
You can access the value of the submit button in your PHP using $_POST['genre'] (or whatever the name attribute of your buttons is)
The images should have an href tag that redirects to your PHP page. Note that this method will mean that you should get the value through $_REQUEST variables and not POST or GET. For example lets say href='myphp.php?genre=horror'. and in the PHP file $genre = $_REQUEST['genre'];
You could try something similar to the following:
<input class='genre' type='image' src='/images/genres/horror.jpg' data-genre='horror' />
<input class='genre' type='image' src='/images/genres/sci-fi.jpg' data-genre='sci-fi' />
<input class='genre' type='image' src='/images/genres/chickflick.jpg' data-genre='chickflick' />
<script>
Array.prototype.slice.call( document.querySelectorAll('input.genre') ).forEach( function(input){
input.addEventListener('click', function(e){
e.preventDefault();
location.href='info.php?genre='+this.dataset.genre
});
})
</script>
Assign each input a dataset attribute which you query later in the click handler. That dataset value is then used to construct the url...
Alternatively a slightly different approach would be to POST the data by setting the value of a hidden field to the dataset attribute value- like:
<form name='genres' method='post'>
<input class='genre' type='image' src='/images/genres/horror.jpg' data-genre='horror' />
<input class='genre' type='image' src='/images/genres/sci-fi.jpg' data-genre='sci-fi' />
<input class='genre' type='image' src='/images/genres/chickflick.jpg' data-genre='chickflick' />
<input class='genre' type='image' src='/images/genres/thriller.jpg' data-genre='thriller' />
<input class='genre' type='image' src='/images/genres/adventure.jpg' data-genre='adventure' />
<input class='genre' type='image' src='/images/genres/period-drama.jpg' data-genre='period-drama' />
<input type='hidden' name='genre' />
</form>
<script>
let form=document.forms.genres;
let genre=form.genre;
Array.prototype.slice.call( document.querySelectorAll('input.genre') ).forEach( function(input){
input.addEventListener('click', function(e){
e.preventDefault();
/* set hidden input value */
genre.value=this.dataset.genre;
/* append the genre to the qction/querystring */
form.action='?genre='+this.dataset.genre;
/* go */
form.submit();
});
})
</script>
I have HTML form with three Submit Buttons.
One is for reloading the page, the other one makes a database entry and the last one is a simple login button. Each button is also in a div.
When i try to login, i normally enter my username & password and then press enter (Im not tabbing out of the textbox). But instead of pressing the button "login", it presses the button "reload".
I found some ways with Javascript, JQuery and AJAX, but im not allowed to use any of these. All i use is PHP, CSS and HTML.
Is there a way to choose which Button i want to press when i use enter?
edit:
This is the form with the three submit buttons:
<form method="post"action="Sub.php">
<input type="text" id="txt" name="txt" value="text">
<input type="submit" id="refresh" name="refresh" value="Refresh">
<input type="submit" id="update" name="update" value="Update">
<input type="submit" id="login" name="login" value="Login">
<input type="text" id="txt" name="txt" value="text"></form>
This is the file, where i post it.
<?php
if(isset($_POST['login']))
{echo "Login has been pressed";}
if(isset($_POST['update']))
{echo "Update has been pressed";}
if(isset($_POST['refresh']))
{echo "Refresh has been pressed";} ?>
If i tab through the first file and press enter in one of the textboxes, i will allways press the submit button with the value "refresh".
Convert your Submit type button to simple button type
$("#textbox_password_id").keyup(function(event){
if(event.keyCode == 13) {
$("#form_submit").submit();
}
});
Other way you can place your Login button first, so whenever you will press it will take first submit click
Duplicate of How do I use two submit buttons, and differentiate between which one was used to submit the form?
Give each input a name attribute. Only the clicked input's name attribute will be sent to the server.
<input type="submit" name="reload" value="Reload"/>
<input type="submit" name="database" value="Database Entry"/>
<input type="submit" name="login" value="Login"/>
<?php
if (isset($_POST['reload'])) {
// Reload-button was clicked
}
elseif (isset($_POST['database'])) {
// Database-button was clicked
}
elseif (isset($_POST['login'])) {
// Login-button was clicked
}
else {
// Failure
}
?>
I'm wondering how to resolve an issue where I have one text box and two buttons.
Each button needs the same data in the text box to accomplish its task.
One button is to update the existing record they are reviewing (with the new value in the text box), and the other button is used to add a new record (again, using the new value in the text).
One idea I had was to use jquery to update a hidden text box that gets updated when the visible text box is modified by the user.
So something like this: (this is just pseudocode...)
<form name="form1" method="post" action="controller1/method1">
<input type=text name=visibleTextBoxForForm1></input>
<button type=submit value=UPdate>
</form>
<form name="form2" method="post" action="controller2/method2">
<input type=hidden name=hiddenTextBoxforForm2></input>
<button type=submit value=New>
</form>
<script>
$('#visibleTextBoxForForm1').live('change', function() {
//update a hidden textbox in form2 with value of this textbox.
});
</script>
Is there a better way to do this?
Alternatively, you could do it via JQuery. Tie a clicklistener for each button and provide the correct URL to the form on click.
Here's some quick code... you'd have to correct the proper jquery queries for the correct elements.
<form name="form1" method="post">
<input type=text name=visibleTextBoxForForm1></input>
<button type=button value=Update>
<button type=button value=New>
</form>
<script>
$('update').click(function() {
$(form1).attr('action', <update url>).submit();
});
$('new').click(function() {
$(form1).attr('action', <new url>).submit();
});
</script>
If that's the only field, then simply have one form with two buttons and handle that text data based on the name of the button used to submit it.
<form name="form1" method="post" action="controller1/method1">
<input type="text" name="text" />
<input type="submit" name="insert" value="Insert New Data" />
<input type="submit" name="update" value="Update Existing Data" />
</form>
PHP (not CodeIgniter, since I'm not familiar with that framework):
if(isset($_POST['insert'])) {
// insert $_POST['text']
} else if (isset($_POST['update'])) {
// update $_POST['text']
} else {
// error
}
I have a form with two submit buttons. based on the button clicked i've to process the values on the post page. so i set the value for action attribute on the button click.
In firefox, it posts form fields and the submit button. but in chrome it only posts the form fields and not the button.. This is the code i've used:
<html>
<script src='jquery-1.6.2.min.js'></script>
<script type="text/javascript">
$(document).ready(function(){
$('#one,#two').click(function(){
$("#myform").attr("action", "index.php");
$("#myform").submit();
});
});
</script>
<?php
echo "<pre>";print_r($_POST);echo "</pre>";
?>
<form method="POST" name='myform' id='myform' >
<input name="iint" value="hiox" type="text">
<input name="one" value="hiox" type="submit" id='one'>
<input name="two" value="hiox" type="submit" id='two'>
</form>
</html>
On post firefox outputs like this:
Array
(
[iint] => hiox
[one] => hiox
)
And in chrome14 [linux version],
Array
(
[iint] => hiox
)
I need to check what button gets clicked. Any help, greatly appreciated. Thanksl!
try simulating the button click with function $('#buttonelement').click(); instead of using submit();
Ah, and as click(); is a standard javascript funcion, you maybe you need to use use GetDocumentByID instead of #id jquery selector.
$('#ElementClickedThatCallOne').click(function(){
$("#myform").attr("action", "index.php");
document.GetDocumentByID('one').click();
});
$('#ElementClickedThatCallTwo').click(function(){
$("#myform").attr("action", "index.php");
document.GetDocumentByID('two').click();
});
as a followup for the comment I added:
then (you have another action also) that's because in post you will only have the button that submits the form.. but you submit it with javascript..
in ff it works because the javascript runs after the submit button (I guess)..
you should consider approaching another solution like: make them input type="button" (and not submit), on click set the value for a hidden input that can be used in php.. the value for that hidden input will always be available, but will change depending on the button you click
your form would become something like
<html>
<script src='jquery.js'></script>
<script type="text/javascript">
$(document).ready(function(){
$('#one,#two').click(function(){
$("#myform").attr("action", "index.php");
$("#who_submitted_the_form").attr('value',this.id);
$("#myform").submit();
});
});
</script>
<?php
echo "<pre>";print_r($_POST);echo "</pre>";
?>
<form method="POST" name='myform' id='myform' >
<input name="iint" value="hiox" type="text"/>
<input name="who_submitted_the_form" id="who_submitted_the_form" value="" type="hidden">
<input name="one" value="hiox" type="button" id="one">
<input name="two" value="hiox" type="button" id="two">
</form>
</html>
This is because the submit button is only included in the form submission traditionally when said submit button is clicked.
since you're not submitting the form via the traditional input="submit" button, no value is included.
You can use this method before form.submit(). call the submit button with click function. so, it traditionally calls the submit button.
document.form name.submit button name.click();
I got a newletter php form, so what I need is to hide the html input after the user click the submiting button, my code looks like this:
<form id="addressForm" action="index.php" method="get">
<p id="foarm">
<input type="text" name="address" id="address" placeholder="mail#example.com"/><br />
<input type="submit" value="Notificame" id="gogo" />
</p>
<p id="response"><?php echo(storeAddress()); ?></p>
</form>
Any ideas?
Thanks.
Use jquery to hide the form when the submit button is pressed. Something like
$("form#addressForm").submit(function()
{
$("form#addressForm").hide();
return true;
}
Should do it. You could do it without jquery, just using javascript - basically add an onclick event to change the css of the form to display:none when you click submit. Id probably suggest placing it in a div section for ease.