How could I perform two action on a button click - php

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/

Related

How to send value of radio button to other page

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.

How to create button direct to url that query result from mysql

I have succeeded displaying data from query from mysql. But there is a url that connected to files that has been stored in mysql database. But I need to changes this url that text based to a button. Here is my script:
<td align="center"><a href='<?php echo $res['url_new_edition'] ?>'>New Edition</a></td>
Now I want to changes the 'New Edition' texts of course as a link/url into a button. I was already tried to add 'class=button', type='button' ect but none of works.
try this
<input type="button" onclick="window.location.href='http://google.com'; return false;">
in your case
<input type="button" onclick="window.location.href='<?php echo $res['url_new_edition'] ?>'; return false;">
Use a form instead of a href if you want a button. A clear solution that even comes along without JavaScript would look like this:
<form action="<?php echo $res['url_new_edition']; ?>" method="get">
<input type="submit" value="New Edition" />
</form>

href hyperlinks as POST

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); ?>

HTML - forms best practice

It's OK to have a form with more submit buttons?
I mean I have a form which contains some input fields and a table and
3 buttons which will export some information in different formats
<form id="form1" name="form1" method="post" >
<label for="country"><strong>Countries: </strong></label><br />
<select id="country" name="country">
<?php
//some php code here
?>
</select>
<br />
<br/>
<label for="start_date"><strong>Start date: </strong></label><br />
<input name="start_date" type="text" id="start_date" value="<?php if(!empty($start))echo $start; ?>" size="20" />
<br />
<br />
<label for="stop_date"><strong>Stop date: </strong></label><br />
<input name="stop_date" type="text" id="stop_date" value="<?php if(!empty($stop)) echo $stop; ?>" size="20" />
<br />
<br />
<input type="submit" name="fill_table" value="Retrieve data" />
<div id="table">
<h1> </h1>
<hr />
<h2>Sales Report</h2>
<table class="display" id="data_table" >
<thead>
<tr>
<th>Product</th>
<th>Date</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
//some php code here
?>
</tbody>
</table>
<br />
<br />
<br />
<input type="submit" class="button" name="export_xls" value="Export xls" />
<input style="margin-left:10px;" type="submit" name="export_html" value="Export html" />
</form>
Is it ok to do it this way or should i make 3 forms each one having a particular submit button? Each time i press a submit button I'm intrested in the input fields :start_date, stop_date and the selected item from "country".
P.S. I want to know if it's optimal and how other programmers would handle this stuff
The main problem is: What happens when the user presses Enter in a text input field? On most browsers, the form data gets submitted, but does this correspond to pressing one of the buttons, and which one? See e.g. the question Multiple submit buttons in an HTML form.
It would be safest to have just one submit button per form. (However it would be safe to add submit buttons with identical functionality into a long form, for user convenience.) This means that the user would make a choice between alternatives (e.g., output formats) by using radio buttons or a select menu, not by a choice between buttons. This is not always practical, though.
I don't think it's a bad idea. But all your submit buttons must have the same name attribute.
Check out this
Yes, you can have more submit buttons in one form. Since you're using php to process the submited form, I wouldn't put the same name for each submit button as #NoZip suggested, but different instead, so that then in the php processing part of the code I would ask:
<?php
if (isset($_POST["submitButtonName1"]))
some code here...
else if (isset($_POST["submitButtonName2"]))
some other code here...
else
code logic for no submit button clicked
?>
I have just finished a system in which you cna update data records, one of the features I used was to use mulitple submits to allow the user to 'stamp' the update with different options. e.g 'active','pending', 'disable'. It saved a drop down box of multiple options and kept the system intuitive.
Multiple submit buttons with different values allow you to submit the form with the submit field having one of many values and can imrove a user interface. Plus, its valid HTML.

Using html radio buttons (or alternative) to call a php function without javascript

Is it possible to call a php function when an html radio button is selected?
I'm working on building a donate page for a small non-profit school that will have three options: Donate, Pay Tuition, or Make a Monthly Donation.
I would like for the choice of the radio button to load the rest of the webpage based on their selection. If possible, I'd like to do this without using javascript (if user disabled) or iframes.
Currently, if I'm using javascript, it works with this code:
<html>
<head>
<script type="text/javascript">
function go (url) {
parent.frame_name.location = url;
}
</script>
</head>
<body>
I would like to:
<br />
<input type="radio" name="type" value="donate" onclick="go ('makedonate.php')">Make a Donation
<input type="radio" name="type" value="tuition" onclick="go ('paytuition.php')">Pay My Child's Tuition
<input type="radio" name="type" value="monthly" onclick="go ('makemonthly.php')">Become a Monthly Sponsor
<iframe name="frame_name" frameborder="0" width="600" height="300"></iframe>
</body>
</html>
Is it possible? Any thoughts?
Put it in a HTML <form> element and add a <input type="submit"> button. Let the form's action point to a single PHP page. In the PHP function behind the PHP page, determine the radio button pressed based on the parameter name and/or value and finally display the rest of page conditionally based on the selected radio button using if-else or switch statement.
Hope this help
Just change the stuff in caps
<?php
if(isset($_POST['donation']))
{
echo "ENTER THE CODE TO BE DISPLAYED IF THEY CLICK DONATION";
}
elseif(isset($_POST['pay']))
{
echo "ENTER THE CODE TO BE DISPLAYED IF THEY CLICK PAY";
}
elseif(isset($_POST['sponsor']))
{
echo "ENTER THE CODE TO BE DISPLAYED IF THEY CLICK SPONSOR";
}
else
{
?>
I would like to:
<br />
<form method="POST" action="ENTER PAGE NAME">
<input type="radio" name="donation" value="donate" >Make a Donation
<input type="radio" name="pay" value="tuition" >Pay My Child's Tuition
<input type="radio" name="sponsor" value="monthly">Become a Monthly Sponsor
<input type="submit" value="Get Info"/>
</form>
<?php
}
?>
In short, No.
You can't assign an action to a radio button change event without JavaScript.
If you add a "Change Type" button after the radio buttons I think you can make it work without any JavaScript, but you will need to use an iframe.
See: How do I submit an html form inside an iframe without changing the outer page?
First way: use flash.
Second way: use wizard style pages and store variables in session.

Categories