I'm wondering which is the best way to map <a href=...></a> hyperlinks performing a HTTP GET to perform a HTTP POST (I'd like to avoid passing all variables in the URL)? For instance, the 2 hrefs below. Furthermore, I would like also to get rid of the submit button and use a regular <a href=...></a> hyperlink. Any suggestions?
<form action="test.php?action=update" method="post" id="cart">
<table>
<tr>
<td>
<a href="test.php?action=delete&id=<?php echo $id ?>" class="r">
remove
</a>
</td>
<td>
add
</td>
</tr>
<tr>
...
</tr>
</table>
<div> <button type="submit">Update</button> </div>
</form>
I'd suggest using jQuery.post and click. When the link is clicked, submit the data via something like this
$('.r').on('click', function() {
$.post('test.php', {key:'value', key2:'value2'}, function(data) {
//error check here
});
return false;
});
As far as i know you can't perform any POST requests with links. You can make your GET requests with links and php as a fallback for users with javascript disabled and for those who have javascript enabled cancel default behavior for links with javascript and make with ajax your POST request with help of AJAX.
for example:
<a class="submit" href="hallo.html?hello=world&test=yes">Test</a>
and js(i used jquery) would be:
$('.submit').click(function(){
var urlPairs = this.href.split('?')[1].split('&'),
total = urlPairs.length,
current = [],
data = {};
for(;total;) {
current = urlPairs[--total].split('=');
data[current[0]] = current[1];
}
$.post('test.php', data);
return false;
});
you could also write your POST function other for more info check jQuery post function doc
P.S. I wounder, if you are using FORM any way why wouldn't you use submit button, is it because of CSS styling? You could style it the same way like any other element(with some tweaks in some browsers).
You can keep the input tags as hidden in your form to submit them as POST.
<td>remove</td>
<td>add</td>
Rewrite the above using two forms, having <input type="hidden" name="id />
and buttons <input type="button" name="delete" />
etc..
if you run print_r($_POST) in your action script. You'll get a list of the buttons and the values of the hidden fields. Now, you can write conditions for the actions to run. I didn't write complete code here, just passing the idea.
Edit: See the example below.
<form method="post" id="cart">
<table>
<input type="hidden" name="id" value='some value' />
<input type="submit" name="action" value="delete" />
<input type="submit" name="action" value="add" />
</tr>
<tr>
...
</tr>
</table>
<div><button type="submit">Update</button></div>
</form>
<?php print_r($_POST); ?>
Related
I have placed my input type reset button inside form but its still not working.
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"><?php echo $rows["news"] ; ?></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /></td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /></td>
</tr>
</form>
I have also tried <button type="reset" value="Reset">Reset</button>
If you're expecting the reset button to empty out the form, that's not the way reset works. It will reset the form fields to what they were when the page was loaded. In this case, you have pre-filled content in your form, so clicking reset will simply undo any user changes since the page-load.
To reset all form fields to blank/nothing, you will need javascript. First get the form DOM object, then iterate over all the input types you want to reset, and set each field.value = '' (input text types / textarea), or modify attributes (for select, radio, checkbox etc.) to deselect and so on. Turn that into a function (there's probably a ready piece somewhere out there, I seem to have lost mine), and attach it to your reset button's click event -- or your form's reset event.
Edit: On a quick search, here's a basic example of how to clear a form to empty values.
Edit: If you're not concerned over anything but text fields, here's a simple way to do this. We add a "reset" event listener to the form, and when it fires, all fields are set to empty string.
function setFormCleanReset(formId) {
let formEl = document.querySelector(formId);
// Add event listener for reset event
formEl.addEventListener('reset', function(e) {
// Iterate all non-hidden fields, set values to ''
for(const fieldEl of formEl.querySelectorAll('input:not([type=hidden])')) {
// #todo check input type and handle "select" etc.
fieldEl.setAttribute('value', '');
}
});
}
// usage: setFormCleanReset('my_form_id');
I think you forgot to insert form tag in your html. It should be work if you insert button code into <form>..button code here..</form>.Something like this:
<form>
<input type="text">
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</form>
You should place the <input type="reset"> in a form like this,it will definitely work
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tring Reset</title>
<style>
</style>
</head>
<body>
<form>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type=reset></input>
</form>
</body>
</html>
You just need to put your reset button inside form. In following code I have taken one input box also an an example. I hope it helps.
<form>
<input type="text" />
<table>
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</table>
</form>
can you please try without echo anything inside textarea
try this
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /> </td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /> </td>
</tr>
</form>
I think reset button is working fine.
Excuse Me! You are using default value in input attribute. Remove default value from it. Then type anything then click rest it will make your text box empty.
I have a form submitting variables to another PHP file. In the second file, there is a button that leads back to the first file for eventual correction od submitted values. The values are retained in the form by $_POST. The "Reset" button works only when the form is used the first time (empty). After submitting and return from the second PHP file, the "Reset" doesn't do anything, no values are cleared. The button seems to be fully inactive.
<button title="<?php echo $lang['reset-button-hint']; ?>" alt="TOOLTIP 1" type="reset" name="reset"><?php echo $lang['reset-button']; ?></button>
#Jaspreet Kaur You need to add form tag outside the table. Please check my previous comment for it.
I have a table with radio buttons to get the row values and 2 buttons
1 button.)For printing data , which moves to "notice.php"
2 button.)For row details,which stays on the same page.
<form action="" method="POST">
<table border="1" >
<tr>
<th>sourceID</th>
....
<th>Status</th>
</tr>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
<td>1</td>
....
<td>open</td>
</tr>
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
<input type="submit" name="details" value="details" />
<?php
if(isset($_POST['details']))
{
$n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
}
Notice.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];
}?>
The problem here is: My code is working perfectly fine with the button details.
But it doesnt work with issue, i.e after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\XAMPP\notice.php.
kindly help
Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.
You could include the data on the query string and send it that way, e.g.:
location.href="notice.php?id=<?=$tot?>"
Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.
Using the following code is the exact same as having a link:
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
As in, this will not change the form action and submit the POST data to your new page.
You would need something like:
<form method="post" action="" name="unique-form-name">
<input type="radio" name="ID[]" value="<?php echo $tot; ?>">
<input type="button" id="unique-btn-name" value="Issue Notice">
</form>
<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
document['unique-form-name'].action='notice.php';
document['unique-form-name'].submit();
}
</script>
Then, once you get the data to notice.php, you'll have to use the data as an array (you won't be able to echo the data):
$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />
Your error is the name attribute.
Also the other button is not related to the form at all. You may want to use ajax here.
This is a bit weird but this time I have came across something that works on IE and not on other browsers like firefox and chrome..
Here is the issue:
I am dynamically loading a part of a page, that contains a form, into my existing page with jquery .load() method.
I have used .on method to attach event handler for the newly added elements.
But when I click on submit button the form submit method works fine, but it doesnt send any data in post query ( by the way, I have specified form method=POST")
The main issue is that before I fire the .load() method to obtain new elements and replace the existing ones, the .submit() works FINE. IT SENDS THE POST DATA.
But after the dom is replaced, there is no data in POST.
jQuery Code:
$(document).on("click", ".s_edit",function()
{$(this).parents('tr').children('form').submit();});
.s_edit is the form submit button:
PHP/HTML code (CodeIgniter):
<tr>
<form method="post" action="<?php echo base_url();?>index.php/userlist/inline_edit/<?php echo $r['id'];?>">
<td><input class="record_edit" type="text" name="name" id="name<?php echo $r['id'] ?>"/></td>
<td><input class="record_edit" type="text" name="age" id="age<?php echo $r['id']; ?>"/></td>
<td>
<span class="record_edit">
<input id="gen_m<?php echo $r['id']; ?>" type="radio" name="gender" value="m"/>Male<br/>
<input id="gen_f<?php echo $r['id']; ?>" type="radio" name="gender" value="f"/>Female
</span>
</td>
<td><input class="record_edit datepick" type="text" name="joining_date" id="joining_date<?php echo $r['id']; ?>"/></td>
<td>
<div class="record_edit">
<input type="submit" value="save" class="s_edit"/>
<input type="button" class="cancel_edit" value="Cancel"/>
</div>
</td>
</form>
</tr>
Because you have <tr><form><td>, other browsers are stripping the form element out, or re-arranging it in the DOM tree.
I.E. In Firefox,
<table>
<tr>
<form>
<td>a</td>
</form>
</tr>
</table>
goes to
<table>
<tbody><tr>
<form></form>
<td>a</td>
</tr>
</tbody></table>
Hence, your form is no longer wrapping the elements in the td.
Ok..I replaced the table formatting with div and table display properties in css..firefox and mozilla as Benno suggested was getting rearranged except for IE..still it somehow worked before invoking .load method of jquery..but now its working completely fine..
I have a page with some input fields and a table and three button play , pause , stop respectively.I want to refresh the table part only when iI press the play button. I want the play button to perform two action one is to run a update query and reload the page only. I when I click on the pause button I want to stop the reloading of the table .And when I press the stop button I want it to run a udate query . I am using codeigniter, MVC architecture. This what I tried and my view page.
<meta http-equiv="refresh" content="300">
<input type="text" size="7" id="IC" name="IC" value="<?php echo set_value('IC',$IC); ?>" />
<input type="text" size="7" id="ID" name="ID" value="<?php echo set_value('ID',$ID); ?>" />
<button type="button" style="width:100px;">PLAY</button>
<button type="button" style="width:100px;">PAUSE</button>
<button type="button" style="width:100px;">STOP</button>
<table>
<tr>
<th> blah </th>
<th> blah </th>
<th> blah </th>
</tr>
//do some code
</table>
You can call two js functions at a time on click of button. like a way..
function jsfun1()
{
//do the action here
}
function jsfun2()
{
//do the action here
}
call both on onclick
<button type="button" style="width:100px;" onclick="jsfun1(),jsfun2()">play</button>
Do it with javascript.
Just make 2 functions and use onclick-action in Button tag.
onclick="playButton0(),playButton1()"
You can use jQuery framework to do all this things.
blind .click function for every button and do stuff with $.ajax function
With ajax function you can post values to controller you want and perform a query or something you want.
more info:
http://api.jquery.com/click/
http://api.jquery.com/jquery.ajax/
Here I used JavaScript to Delete an employee...
<script type="text/javascript">
function delet(emp)
{
var answer = confirm('Are you sure?');
if(answer)
{
window.location='delete.php?emp='+emp;
}
}
</script>
By using anchor tag am calling the function of javascript ....
<a onclick="javascript:delet('<? echo $_GET['emp']; ?>')">
<input type="button" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value=""/></a>
But my problem is it is working upto showing the alert msg but after answering the alert msg it is not redirecting to the page given by me that is "delete.php?emp=+emp"
Because you're nesting an <input type="Submit"/> inside an <a>, clicking the button is not the same as clicking the <a>, rather it "Submits" nowhere (read to the page you're already on, refreshing the page) before the <a> can do it's job.
Simple demo where you don't go to google.
<input type="Submit"/>
Bergi has pointed out that the behaviour I described is not universal (hello Opera, IE) unless a <form> element is present, so for example the following
<form action="jail.php">
<a href="go.php" onclick="window.location='?collect=£200';">
<input type="submit"/>
</a>
</form>
will send you directly to jail without passing go or collecting £200 across all browsers.
Further, this only really applies to page redirection; other pieces of script may well fire before the page changes, the easiest to observe being console.log, alert, etc.
You're submitting some form by clicking on that button. That submit process will overrule the window.location navigation request. A quick workaround would be to prevent that:
<a onclick="event.preventDefault(); delet('<? echo $_GET['emp']; ?>');">
// cross-browser-safe:
<a onclick="delet('<? echo $_GET['emp']; ?>'); return false">
but actually your markup with the nested button is quite odd. You don't need that link at all, just use
<form action="delete.php" onsubmit="return confirm('Are you sure');">
<input type="hidden" name="emp" value="<? echo $_GET['emp']; ?>" />
<input type="submit" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value="" />
</form>