I want to display a data table when i click a button in the same php page. The button is used in a form with other inputs such as some text. The data table is hide by default. And it get the values from the form, and then make a query in database and display them in it.
How can i achieve the function of display/hide ?
Do you have any solutions?
Thanks.
its a simple ajax issue, you can use any popular java script library to achieve this functionality to perform ajax calls and show/hide table.
for example, you can use jquery's ajax functionality to get the data from the server and then
use jquery's built in effects to show the table enclosed in div. For example, to display the content in the div 'mydiv', simply write
$("#mydiv").show(); And to hide the content, write $("#mydiv").hide();
If you're okay with using Javascript, give your table tag an id attribute. Then, have an onclick event on the button that alters the CSS of the table to be display:none. Alter the Javascript of the button so that the next time it is clicked, it toggles the table CSS to be display:table. You could also use a Javascript library, such as Prototype, to do this.
<table id="myTable">
</table>
<input id="toggleButton" type="button" onclick="hideTable(true);" value="Toggle Table" />
And the Javascript might be:
function toggleDisplay(var hide) {
if (hide) {
document.getElementById('myTable').style.display = "none";
document.getElementById('toggleButton').onclick = hideTable(false);
} else {
document.getElementById('myTable').style.display = "table";
document.getElementById('toggleButton').onclick = hideTable(true);
}
}
Take that Javascript with a grain of salt; I haven't written any in a while.
If you don't want to use Javascript, then have the button submit a regular HTML form. Pass along in the form some input name such as hide_table with a value of true. On the server, if $_POST['hide_table'] == "true", don't allow the table to be output as HTML to the page. Also, toggle the form such that clicking the button will submit hide_table with a value of false.
<form method="post" action="the_same_page.php">
<input type="hidden" name="hide_table" value="<?php echo $_POST['hide_table'] == "true" ?>" />
<input type="button" value="Toggle Table" />
</form>
<?php if ($_POST['hide_table'] != "true") { ?>
<table>
</table>
<?php } ?>
If you wanted to use AJAX to only load the table content when the user decides to show it, it would be nice to make this degrade gracefully. If a user doesn't have Javascript enabled, the form should actually submit to the server and reload the page, toggling the table display. However, if the user does have Javascript enabled, an AJAX call could be made, load the table, and display it in-place.
Assuming that you want to do this client side (ie all data is sent to the client on page load) all you need to do is something thusly: (done with prototype for brevity)
...
<input type="button" id="showTableBtn" value="Show Table">
<table id="dataTable">
...
</table>
<script type="text/javascript">
<!--
Event.observe($("showTableBtn"), "click", toggleTable);
function toggleTable() {
if ($("showTableBtn").value == "Show Table") {
$("dataTable").show();
$("showTableBtn").value = "Hide Table";
} else {
$("dataTable").hide();
$("showTableBtn").value = "Show Table");
}
}
//-->
</script>
...
Related
I am attempting to display a confirmation box, using Jquery, after a user has clicked a delete link. When the delete link is clicked it sends the post_id to the url. As long as this is set and it isn't empty I trigger the animation which will display the hidden confirmation box.
This is what I have so far which isn't currenlty working:
// if admin wants to delete a post check for post_id
if(isset($_GET['post_id']) && !empty($_GET['post_id'])){
$delete_id = (int)$_GET['post_id'];
$animate = true;
echo '<script type="text/javascript">';
echo 'var animate = '.$animate;
echo '</script>';
}
The GET variable is set correctly.
In my jquery file I have:
$(document).ready(function(){
if(animate == true){
$("#delete_confirm").fadeIn('3000','swing');
}
});
and the confirmatino box:
<div id="delete_confirm">
<p>Please confirm you want to delete this post.</p>
<input type="button" id="delete" name="delete" value="confirm" />
</div>
Which has display:none; set in the style sheet.
Why does it not display when the animate variable is set to true?
Thanks
UPDATE:
Try this, tested and working:
<?php
if(isset($_GET['post_id']) && !empty($_GET['post_id'])){
$delete_id = (int)$_GET['post_id'];
$animate = true;
}
?>
<html>
<head>
<title>Simulation</title>
</head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var prop = {
animate: "<?php echo $animate; ?>"
};
$(document).ready(function(){
if(prop.animate == 1){
$("#delete_confirm").fadeIn('3000','swing');
}
});
</script>
<body>
<div id="delete_confirm">
<p>Please confirm you want to delete this post.</p>
<input type="button" id="delete" name="delete" value="confirm" />
</div>
</body>
</html>
You should unset the display:none; property on the div and either call .fadeOut(0) on page load, or set its opacity:0.0; in the CSS file.
Additionally, instead of including all of this server side code injection stuff, you could handle the GET parameter check on the client side by using the window.location object's search property, then detect if a particular parameter is set with a regex match. That way you don't have to use any inline JavaScript.
var matches = window.location.search.match(/post_id/g);
if(matches && matches.length > 0)
{
// do animation
} else {
// do something else
}
Something like this should work, and personally I like doing this more than injecting script tags and inline JavaScript.
The php checks is the GET variable is set, if it is it sets $animate
to true. After this the Jquery does the check on the animate variable,
if its set to true then the fadein should happen.
For me this is very confusing. Let me try to research this a little bit more.
You have a page with the confirmation box.
When user press delete button the confirmation box (<div id="delete_confirm">) is show using jQuery script.
User press the 'delete' button on the page. What happens here? There are two options:
You do AJAX request.
You do HTTP GET request.
Based on your comments and the code I can guess that you do HTTP GET request. In this case you reload the page. Yes, PHP gets the proper value in $_GET['post_id'] and the post gets deleted.
But you get completely new HTML page. And on this page the div <div id="delete_confirm"> is invisible from the very beginning. This is why it is not shown and hidden.
When you look at the source code you can find you JS script <script type="text/javascript">... and I beg that the code $(document).ready(function(){... works too. But you get it on newly loaded HTML page.
Please correct me if I get you wrong here.
I think that you want to hide the confirmation box you might want to use AJAX. But then you might also want to remove the post from the page by altering the page.
Quoting the jQuery documentation: "The .fadeIn() method animates the opacity of the matched elements."
What .fadeIn() will do, effectively, is change the css opacity of an element from 0 to 1.
You've stated that in the CSS your element has display: none; so all you're actually doing is changing the opacity from 1 (as assuming you've not set that in the CSS) to 1 whilst leaving the display set to none ... so it'll never be displayed.
This seems to hold true for .animate() which doesn't allow you to "animate" the display type (from "none" to "block" - you'd use opacity)... but not for .fadeIn() / .fadeOut() which do allow you to fade a block in/out using the CSS display property.
My bad, I was confusing the two.
I am trying to submit a form using a hyperlink and it is not posting values onto the next page.Here is my code form:
<?php
$email = array('name'=>'accountno','id'=>'accountno','value'=>set_value('email'));
?>
<form method="post" id = "login_form" action="/salesrep/check" name = "login_form" class="custLogin">
<fieldset style="color: #BD1313; width: 440px;"> <input type="hidden" name="submit_type" id="submit_type" value="account_only">
<br><center><label for="customerNo"><b>Customer No:</b></label>
<? echo form_input($email);?>
Submit<? echo form_input($button);?> </center>
<p> </p>
</fieldset>
</form>
The code on the next page looks like this:
<?
print_array($_POST);
die();
?>
When i use the button here,it posts values to next page successfully.BUT I HAVE not been able to post values using the hyperlink on onclick event. Where i am making mistake??
Why i am getting empty array when i am already inserting value in the text box.?? Or is there any way i could post values using the link and retrieve them in the next page???
The real problem is that you are trying to use a link plus some JavaScript to submit a form in the first place. Don't do this. Buttons inform users that they will submit the form. They will show up in screen readers when they are in forms mode (a link with some JavaScript won't). They "just work".
If you insist on using a link and some JavaScript, then the reason that your code doesn't work is that the JavaScript runs, the form starts to submit, then the link is followed and a GET request is made to the page instead.
Normally you could call preventDefault on the event to stop this, but you are using old style intrinsic event attributes so you need to return false; from there instead.
Recommended reading: Progressive Enhancement and Unobtrusive JavaScript
<?php
$sort=$_REQUEST['sortby'];
echo" $sort ";
?>
<html>
<head>
</head>
<body>
<form method="get" id="thisForm" >
<table border=1>
<tr>
<td>
Day
</td>
<td>
Server
</td>
<td>
Service
</td>
<td>
Count
</td>
</tr>
</table>
<input type="hidden" name="sortby" id="sortby"/>
</form>
</body>
<script type="text/javascript" >
var sortArray=new Array("say","server","service","count");
function sorting( cnt)
{
alert(sortArray[cnt]);
document.getElementById("sortby").value=sortArray[cnt];
}
Based on the Header clicked i am trying to build a sort query but after it is submitted on clicking the "a href" link i am unable to get the hidden field posted data ?
I have changed "a href" to submit buttons then how do you stop the page being submitted if we click the same "sort by" link.
Here is what you're missing:
A link does not submit the form with input data, it just changes the url.
Submit the form.
Override the default <a> behavior (for example, using return false).
An updated version of sorting:
var sortArray=new Array("say","server","service","count");
function sorting(cnt)
{
document.getElementById("sortby").value = sortArray[cnt];
document.getElementById("thisForm").submit();
return false;
}
You can call it using:
Day
Working example: http://jsbin.com/ukoton/3
Another option is to use the popular library jQuery, which can greatly simplify your code:
$(function(){
$('#thisForm a').click(function(ev){
var sortValue = this.name;
// an alternative to this.name is to use a data-sortBy='day' attribute,
// and then $(this).data('sortBy')
$('#sortby').val(sortValue);
$('#thisForm').submit();
ev.preventDefault();
});
});
Working example: http://jsbin.com/ukoton/4
By default, anchor tags (that being A) do not submit a form. You can either use a button or submit the form using javascript.
You can submit the form from javascript with document.thisForm.submit()
Hmm .. I am not a php guy .. so I guess that initial 4 lines tries to get the sortby field from the form, anyways href link is not equivalent to html form submit, you will need to explicitly submit the form, maybe using javascript, to get the value
Just use a Submit button and style it to look like a link, if you need to. Using Javascript shouldn't be needed here.
Using an actual link instead of a Submit button will make the form unusable for people without javascript. That might include people using accessibility tools. Apart from that, it makes your page more complex (adding illogical HTML, and requiring Javascript), and therefore more complex to maintain.
I have a simple form which accepts a Title and a Contents variable from a textbox and a textarea. The form will send its data to a file called add-post.php. However, I am looking for a way to alert the user that either the textbox or the textarea has invalid data (is empty) in case they click the submission button.
I was thinking that an alert() popup box would be the best idea because it doesn't redirect to any other page and the user never loses their data (imagine they entered a whole lot of text but forgot a title. Sending the data to add-post.php and performing the check there will result in loss of data for the user).
However, I'm not sure how to actually implement the alert() popup. How would I make it so that the check is done AFTER they have clicked the submit button but BEFORE the data is sent off to the next file. Any advice is appreciated.
On your form add something like this
<form name="frm1" onsubmit="InputChecker()">
Then in javascript
<script type="text/javascript">
function InputChecker()
{
if(document.getElementById({formElement}) != '') { // not empty
alert("This element needs data"); // Pop an alert
return false; // Prevent form from submitting
}
}
</script>
Also as others have said jQuery makes this a little bit easier. I highly recommend the jQuery Validate Plugin
Some people do find the alert box "annoying", so it may be better to append a message into the DOM to let the user know what needs to be fixed. This is useful if there are numerous errors as the errors will be more persistent allowing the user to see all the things they need to be fixed. Again, the jQuery Validate plugin has this functionality built in.
Attach an onsubmit event to the form, and return false; to stop the submission if checks fail.
Form validation with Javascript. Or easier with jQuery.
Basically, validate the form when the submit button is clicked (with an onsubmit handler), and then use an alert() box if needed. By the way, people usually hate alert boxes.
You have a number of options when it comes to client side validation. This is just one.
<form id="tehForm" method="post">
<input type="text" id="data2check" >
<input type="button" id="btnSubmit" />
</form>
<script type="text/javascript">
function submit_form(){
if(document.getElementById("data2check").value!="correct value"){
alert("this is wrong");
}else{
document.getElementById("tehForm").submit();
}
}
</script>
For a more indepth example check out this link
I am trying to create a list of links that would make up the elements of a form (will be used as a search feature).
Basically, each link in the list represents a search category, so as users click on a link, their search results will be filtered.
I would need a category value to be able to be passed whenever someone clicks on one of those category links, and also have each link act as the form submit as well. Is that possible?
(There may be easier ways to accomplish this, however with the CMS and search module I'm using, this will have to do.)
Are you trying to pass values via both POST and GET? I am not sure that that would work. Why not just used hidden form fields? In your form, add a number of <input type="hidden" name="foo" value="bar"> and each one of them will be passed back to the server along with all the regular form fields.
Note: Hidden form fields can be read and edited by a sufficiently tech savy user (it is not too hard), but this would be the case with any variables that you are passing between the server and client, even cookies.
If you give the links an internal data-category attribute like this:
Foo
then you can do with jQuery:
$('a[data-category]').click(function() {
filterResults($(this).data('category')); // call the function that filters
// results according to chosen
// category
$('#theform').submit();
}
You can do something along the lines of...
Puma
and use the a script like...
$('a.category').click(function(e) {
$('#someForm input[name=category]').val( $(this).text() );
$('#someForm').submit();
});`
I'm not 100% sure what you intend to do with the data from the link, or even which part of it you need, but I hope this helps.
<form name="myForm">
link
</form>
<script>
function submitMyForm(link){
var mylinkText = link.text;
var myLinkHref = link.href;
document.myForm.submit();
}
</script>
If you need the link clicked to come across as part of your form data, perhaps you could put it in a hidden field on the form... something like:
document.myForm.myHiddenLinkField.value = mylinkText;
Since the jQuery tag has been removed, here's an example of doing this form submit with pure javascript:
http://www.neubreed.com.au/blog/2010/07/submit_form_anchor_tag_using_javascript_and_supply_action
You can create an extra hidden input like in the example to set your category:
function submitForm(id, category){
var myform = document.getElementById(id);
if (document.createElement) {
input = document.createElement('input');
input.type = 'hidden';
input.name = 'category';
input.value = category;
myform.appendChild(input);
}
myform.submit();
return false;
}
Then, in your form:
click
You could work with a hidden field.
<form id="cuteform" action="thedestiny.php">
<input type="hidden" id="filter"/>
</form>
<a onclick="javascript:linkaction(this);" id="linkA">blablabla</a>
<a onclick="javascript:linkaction(this);" id="linkB">blablabla</a>
<script>
function linkaction(link) {
// get the field
var f = document.getElementById("filter");
// then you could use some of the link element's property like id or innerHTML
f.value = link.id;
// or you could use a switch structure
switch (link.id) {
case "linkA": f.value = "the huge filter string"; break;
case "linkB": f.value = "another stuff"; break;
}
// submit the form
document.getElementById("cuteform").submit();
}
</script>
I'd be inclined to style an actual form button as a link using CSS.
So essentially for each "link":
<form method="get">
<input type="submit" class="form-link">
</form>
For the CSS (off the top of my head) something like this should do it:
.form-link {
background-color: transparent;
border: none;
text-decoration: underline;
color: #00f;
}
That way it isn't dependent on JavaScript to work and users without it will still be able to make use of your site. Additionally, inline event handlers (onclick) etc shouldn't be used as they mix semantics with behaviour. If the functionality isn't core to the experience and (perhaps therefore doesn't require a base non-JavaScript version) then write the links into the page with JavaScript from an external JavaScript include. You won't then have a bunch of dead links in the page for users that visit your site with JavaScript disabled or from a non-JavaScript capable device.