So hello everyone I have a big problem right now with a button that is made depending on the variables that are sent to the page in this case it's a button the when it's clicked it will access a DB and extract everything in the DB and put it into excel format. The problem right now is when the button is made it always execute the DB search and file making. I know it has to do it because of the way the button is made right now, is there anyway of changing this?
if ($action_db) echo "<button class=\"btn\" onClick='".getUDB()."'>" . DBUser . "</button>";
to this:
if ($action_db) echo "<button class=\"btn\" onClick=\"getUDB()\">" . DBUser . "</button>";
and sending the event trough java-script so it executes the getUDB() method that's on my PHP file?
Thanks.
OK so I went and used part of the suggestion that Sam said and put it on a JQuery and the change is like this
if ($action_db) echo "<button class=\"btn\" id=\"UDB\">" . DBUser . "</button>";
JQuery:
<script type="text/javascript">
$(document).ready(function(){
$("#UDB").click(function(){
<?getUDB();?>
});
});
OK so the problem is that when I select the clients menu or any other part of the menu to access that part of the webpage it's automatically doing the getUDB method and won't let me access anything else. Any suggestions???
If you don't want the PHP function getUDB() to be called when you load the page, then you're right - you want to move this out into a Javascript function.
By doing echo "foo" . getUDB() . "bar", I'm assuming your page is loading slowly. To solve this, you can use Javascript with an AJAX call. So you probably want to:
Create a separate PHP file that calls getUDB().
Create a Javascript function on your original page that issues a GET request to this new, separate PHP file. It's easiest to do this using jQuery, which makes AJAX requests nice and easy - check out this page.
Write the Javascript needed to handle the response from that page appropriately.
SitePoint has a good tutorial on jQuery and AJAX, check it out.
You need to send a request to the server to initiate anything server-side in response to an event, either by a traditional post-back or via AJAX.
I suggest you make your buttons into forms, substituting the ID of the user for the value of the hidden input:
if ($action_db) {
?>
<form action="/getUDB">
<input type="hidden" name="DBUser" value="<?= DBUser ?>" />
<button class=\"btn\" type="submit"><?= DBUser ?></button>
</form>
<?
}
And then set up a second script to handle POST requests to /getUDB
Related
I have PHP code like this: (for signing out - located in a php page together with other php handlers for different functions and this file is included in pages like index.php or other relevant pages.)
if(isset($_POST['signout'])) { // logout button
// Clear and destroy sessions and redirect user to home page url.
$_SESSION = array();
session_destroy();
// redirect to homepage (eg: localhost)
header('Location: http://localhost/index.php');
}
I normally use a <form action="index.php" method="post"> where the function is currently included and <input type="submit" name="signout"> for such things but this time i would like to use an anchor tag like:
<a href="" >Sign Out</a> for signing out.
Would anyone be kind enough to show an example code that will trigger a submit that will be handled by the given PHP code above.
Either a jQuery or Javascript solution would do. I would just like to see a complete example on how this works.
There's good reason for using a POST for submitting authentication tokens which usually commences or alters the session state - but these don't really apply to the problem of closing the session - why not just just trigger this via a GET?
But if you really must do a POST, and you really must do it via a a href (rather than styling a submit button) and you really must do it via javascript (which will break if the client has javascript disabled) then...
<script>
function sendForm(formId)
{
if (document.getElementById(formId).onsubmit())
document.getElementById(formId).submit();
}
<script>
<form id='logout'>
<input type='hidden' name='signout' value='1'>
</form>
logout
plz anyone help me give me solution to run command like onclick without javascript....thanks
//javascript code:
function selectedproperty(id)
{
document.getElementById("pid").value=id;
document.getElementById("form1").submit();
}
function singleproperty(id)
{
document.getElementById("pid").value=id;
document.getElementById('form1').action="singlepropertydetail.php"
document.getElementById("form1").submit();
}
// html/php code
<ul>
<?PHP
$sql=mysql_query("select build_name,flat_no,property_id from new_property where owner='".$ownerid."'") or die(mysql_error());
while($row=mysql_fetch_row($sql))
{
echo "<li onclick='singleproperty(".$row[2].");' >".$row[0]." ".$row[1]."</li>";
}
?>
</ul>
You put a form with a button inside the li. The user clicks on the button and if js is disabled it posts back to the server and it does the logic presenting the user with the required modification to the page. You JavaScript to prevent the form from submitting and handle the logic client side if js is enabled.
Here is an idea of how it would look
<li onclick='singleproperty(".$row[2].");' >
<form method='post' onsubmit='return false'>
<input name='singleproperty' type='hidden' value='".$row[2]."'>
<input type='submit' value='".$row[0]." ".$row[1]."'>
</form>
</li>
To Pass the values to the server you can do either of the below
Use a submit button which will submit the form to the server. This will hide the parameters from the URL and would use request body.
If your page is just doing read operation it is safe to perform GET request on the page where the parameters will be present in the URL.
If you want to write an app which should work in the absence of JavaScript you must use design principles such as Graceful degradation & Progressive enhancement. Explaining in detail on how to achieve this will be outside the scope of this Question & Answer site.
Or, you can have a separate HTML only site as GMail does.
The decision of whether to support JavaScript less browser or have a separate site or do graceful degradation has to be thought through before the application is written and corresponding compromises has to be made well in advance.
I am trying to make a website that deals with a database of students and tutors. My issue is that so far the only way I know of by which I can run a PHP script (that, for example, removes a user from the database) is putting the information in a link and letting the user click on that link. This does work, but it is annoying because it means that the user is constantly being redirected to new pages. I have also been using some $_SERVER['PHP_SELF']?other information, but this is bothersome because it builds up a long stack of the same page (ie trying to use the Back/Forward function of the browser brings you to the same page).
I would like to be able to generate links/buttons that, when clicked, pass information to a php script without redirecting/changing the page (except possibly refreshing the page to show the new result).
An example from my site, where the page generates a list of all the users and then respective links/buttons to remove each user:
//Gets the list of users and iterates through the data
while ($row = mysqli_fetch_array($data))
{
$fullname = $row['lastname'] . ", " . $row['firstname'];
$username = $row['username'];
$remove_link = "remove_user.php?username=$username";
echo '
<tr>
<td>' . $fullname . '</td>
<td>' . $username . '</td>
<td> Remove this user. </td>
</tr>
';
}
echo '</table>';
When $remove_link is clicked, it loads a php script that removes the user and then redirects back the the original page (user_list.php). Is there a way to just call the remove_user.php script without redirecting/changing pages?
Thanks in advance.
The way websites work today is by using Ajax to dynamically load / change the content of a page.
The best way to use Ajax is to use jQuery's Ajax functions. There are many tutorials on the internet that will show you how to use jQuery.
If you do not want to use jQuery for your Ajax, you can use the old fashioned way of doing it. You can view a tutorial on it on w3schools.com.
Hope this helps!
You need to start familiarizing yourself with javascript and AJAX. Probably look at using jQuery for your javascript framework as it maked this relatively simple and is the most popular such framework, with wide support.
You need to do this via Ajax, one of the easiest ways to do this is with jQuery and its $.ajax, $.post, $.get and $.getJson methods wich will let you do what you want to do.
In case you haven't used jQuery i will recommend you to search for some tutorials especially on nettuts
5 Ways to Make Ajax Calls with jQuery
Read this tutorial, it will help you.
In addition to the answers at the top. In case the user has javascript disabled, I use this AJAX fallback script.
At the end of your deletion script place this:
<?php
//check if the request is not done with ajax
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//now check if the http referer is not empty. It's where the click took place. And if it's not empty redirect back to where the user came from.
if (!empty($_SERVER['HTTP_REFERER'])) {
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
}
}
?>
Instead of <a href="link"> you can use <a onlick="window.location.replace('link')"> which doesn't add a path into the history and you can use the PHP_SELF approach.
Other than that you would have to use AJAX.
I have parsed a page using curl and it contains some check box and a 'select all' and a 'submit' button . Clicking the button selects every check box.'select all' button triggers a javascript function which actually select all check box.
Now i need to click 'select all' and 'submit' button.How can I do this ??
Here is the button code:
input type="button" onclick="SelectAll(137)" value="Select All"
Here is the js function:
function SelectAll(n)
{
for(i=0;i<n;i++)
document.getElementById("ch"+i).checked=true;
}
You can't. If you must use cURL, you'd have to manually craft the POST request yourself. This would involve finding the names of all the checkboxes and sending a bunch of checkbox1=on&checkbox2=on&... in the POST body. See http://davidwalsh.name/execute-http-post-php-curl for a general example of a POST request through cURL with PHP.
Another option would be to use something like Selenium Web Driver, which pretty much allows you to script a web browser like Firefox that can run JavaScript, click things, etc.
Pass a parameter in with your Curl request that gets parsed by jQuery on its $(document).ready(). There's some plugins that help with parsing parameters.
Something like
$(document).ready(function(){
var buttonPress = $.param("buttonPress");
if (buttonPress == true)
$("#myButton").click();
});
Basically I want a confirmation box to pop up when they click the button asking if they sure they want to restart, if yes, then it destroys session and takes them to the first page. Heres what i got...
echo "<form id=\"form\" name=\"form\" method=\"post\" action=\"nextpage.php\">\n";
echo " <input type=\"button\" name='restart' value='Restart' id='restart'
onclick='restartForm()' />";
and for the script...
<script type=\"text/javascript\">
<!--
function restartForm() {
var answer = confirm('Are you sure you want to start over?');
if (answer) {
form.action=\"firstpage.php\";
session_destroy();
form.submit();
} else
alert ('Restart Cancelled');
}
// --
</script>";
EDIT: Note that pressing the button brings up the confirm box, but if you click okay nothing happens sometimes. Sometimes if u click cancel it still submits the form (To the original action)
Unless your session_destroy() method in Javascript actually sends a request to the PHP script or something, it looks like you are trying to put PHP code in your javascript, which will not work.
You should try redirecting them to something like firstpage.php?reset=1 and inside the PHP script, you can check for the reset flag, then call session_destroy().
You should use
onclick='restartForm(); return false;'
to prevent the form from being sent.
Also, session_destroy() is not JS AFAIK...
EDIT: you should also remember that sessions are server-side, not client side, so if you want to manipulate them you cannot do it in JS. The only thing you can do is calling a PHP (e.g. via AJAX) that does something with the sessions.