HTML Button to run a PHP script following a second click - javascript

I am using HTML on a Raspberry Pi to control relays, currently I have a button for the On and another for the Off of the GPIO, I was wondering if there was a way I could press a single button once to call the On.php and press the same button a second time to call the Off.php without leaving the page.
Right now one button moves to the PHP script page which turns the GPIO On and then returns to the home page, the Off button does the same. This is a bad method but it worked.

Seting a global variable to calculate the times of your clicking (flag==0). Then writing something to define which functions you want to use.

I used a checkbox for the input, a function is called when the box is clicked which then checks if it is ticked or not, the code then uses a variable to decide what state the GPIO Pin should be in.

Related

Why razor executes a C# method inside a button click method on compilation without clicking the button?

-ASP.NET 4.7 Web Application-
I hold some notifications in the session to show the user. I want to remove the session variable on button click.
The idea is to make sure the user gets notified so as long as the user don't hit "Got it" button, I won't be removing the notifications from the session so that every time the users reload the page they will see the same notification window.
I have a button in a modal as follows:
<button type="button" class="btn btn-secondary" onclick="clearSession()" data-dismiss="modal">Got it</button>
And this is the method supposed to be called on button click:
<script type="text/javascript">
function clearSession() {
#{Session.Remove("WhatsNewList");}
};
</script>
The problem is that the following part of the code precompiles and executes even though the button is not clicked.
#{Session.Remove("Notifications");}
I want to know what is the reason for this and is there a way to prevent this.
I know I can call a C# method using Ajax or jQuery but I want to know if there is a way I can do it this way as well.
Thanks in advance!
Sourounding an expression Using #{ } will cause the expression to be evaluated server side before the page is loaded into the browser meaning the code will execute as soon as the page is requested. So, the short answer is, you cannot do what you are trying to do using this approach.

Is there a way to create a Yes/No popup in ASP.Net VB by calling a function from the code behind?

I need to check a variable in the code behind of a ASP.Net web page and if the variable is a certain value I want to display a modal popup with a message and a Yes/No button. The message will be sent from the code behind.
You can't really do this. However, you can get much the same effect.
First approach - is very easy
This assumes the user going to click on a button on the form. So, say a delete button, or some action to say run a process.
You drop in your standard asp.net button onto your form like this:
<asp:Button ID="Button3" OnClientClick="return myconfirm();"
runat="server" Text="Delete record" Width="90px" />
<script>
function myconfirm() {
var x = confirm("Do you want to delete this record");
return x;
}
</script>
So the above will FIRST run the "js" script code. Note the "on client click". If that function returns true, then you standard code behind button (server side) will run.
In fact, in the vast number of cases, the user "has" to click on something for action to take place. You really say click on a button, then code behind starts running. However during this so called "round trip", you can't have your code STOP and ask for input such as yes/no.
Why? Because when you click a button, the web page travels up to server. Your code behind starts running. Any thing you change on the web page is now changed with the code behind. And THEN the whole page is sent back to the browser. If you stop or interrupt the code while in code behind, then the web page NEVER will get sent back down to the browser until ALL OF your code behind is 100% done, and is finished. When all that code behind is all done, THEN the page gets sent back down to the browser. So you can't interrupt the code.
There are a few other ways to pop up a dialog. And you CAN have a dialog pop and launch as a result of code behind, but it will in effect be the LAST thing your code does, and this will mean that a script block is send down along for the rid in the web page also being sent down to client side.
And in place of the "lame" confirm() in js, you can certainly use say jQuery.UI which allows you to make some half decent dialog boxes compared to the horrid and ugly and simple confirm dialog box. To be fair, the Chrome browser confirm() does look quite nice, but confirm()/alert() dialogs in most browsers leaves a lot to be desired, and calling them "ugly" is being kind.
The other way to ask/get prompts? Don't use a confirm/dialog. Simply put your yes/no on the form as say radio buttons. If you have 2-3 questions, then simply set auto-post back = true, and after the first yes/no (say a radio button group), then a post back occurs, and you display the next question. This works VERY well I find with code behind.
Last but not least?
You can certainly have code behind pop up a dialog box if it is the LAST thing or LAST line of your code. You can do a registerScipt block, and it can popup a dialog for yes/no. But since it is only the VERY last code you can execute in your code behind, then your code behind in 99% of cases is going to be the result of a user having clicked on a asp.net button. Since that is the case, then the above first example to ask/confirm to run the button code again will suffice in 99% of the cases. So just keep in mind that you can't have ANY blocking code in code behind during that round trip. As a result, you can't have a blocking prompt appear and THEN the code behind continues. The code behind has one shot, one change to run. That code runs while the page is up on the server. Your code runs, changes things, values of controls, and then once done the page travels back to the client side. This is the so called round trip.
As noted, you can certainy in that round trip setup the browser to prompt with a yes/no dialog AFTER the page travels back to the browser. So for example, I do say setup a toast message. If a person say clicks on some button, and say to do something (say download a file), and the file does not exist? I can have the browser say spit out a toast message. So you can in code behind trigger a dialog box, or toast messages and even have a dialog box pop up from the code behind, but just keep in mind that setup of the toast message or popping up of a dialog box will be the LAST action you do in code behind, and then the browser travels back to client, is displayed, and THEN your dialog box pops up. You can (and will) then have code behind (and another round trip) occur as a result of that dialog box popping up. I can post an example of how to do this, but in this exmaple while code behind is setting up and triggering a dialog box, there will be no code blocking, or waiting for a yes/no and then the code block continues.

Java HtmlUnit background JavaScript loading

I have a website I'm trying to navigate using HtmlUnit.
This website changes certain buttons/makes certain elements visible/hidden based on JavaScript events.
The simplest example I can give: there is a text input box and a button with a class on it that prevents it from being clicked. As soon as text is written in the box the button becomes clickable. I am setting the value of the input text box but am not seeing the updates on the button.
I have tried the following in order to make the page wait for background updates:
use NicelyResynchronizingAjaxController() as the web client's ajax controller
override processSynchron() to return true when setting ajax controller
putting the thread to sleep a few seconds
synchronising on the page variable and calling wait on it for a few seconds
calling waitForBackgroundJavaScript on the web client
looping while the page's enclosing window's job manager has no more JavaScript jobs
Looks like you are using a really low level method.
Try
using the type("...") method
then waitForBackgroundJavaScript() to wait for the javascript to process
If this does not help you have to provide a detailed sample to give us a chance to see your code and do some analysis/debugging.

Javascript: Making divs hold state of display='block' when user clicks BACK button in browser

I have a few divs on a form that are hidden by default (style="display:none;"). When the user clicks a certain radio button, an onclick event executes and exposes the divs. The user is then taken to a review page upon form submit that shows him any errors. If there are any, he clicks the BACK button on his browser to go back to the form and correct them. Caching is enabled so that all of his form contents are there. The problem is, since the form is looking for an onclick event, all of the previously exposed divs are hidden again. Is there any way to make sure they stay exposed when the user clicks back to the form from the review page? I thought a document.ready function would do it, but no joy.
As Yair mentioned, you can use cookies. It cannot be done with pure JS. However, you can also use PHP.
Before the user is transferred to the second page, have JS scan the divs in question, and find which ones are visible. (I'm assuming they all have individual IDs). Store these IDs in a comma-delimited string, or array, and send it as a _POST or _GET to the new page.
Have PHP store it as a hidden value somewhere. You could use a hidden input, or a data-x on something ... as long as it's there.
Have JS on that page that watches for the back click, stops it, and then redirects the user to the previous page, and sends the string or array back to it. Have PHP on that page print it as a JS value, and have JS on pageload show all divs with matching IDs.
Cookies or localStorage if you aim for only modern browsers:
localStorage
Is there any way to make sure they stay exposed when the user clicks
back to the form from the review page? I thought a document.ready
function would do it, but no joy.
You can use cookies in order to manage state in a web-browser. Cookies will help you save the desired user's state.
All javascript code is reinitialized on browser reload. You cannot identify whether the user comes back through the browser.
You can use cookies or local storage to save a value when initial display happens and show/hide the div later on document.ready.

Simulate click on DIV

I am developing a desktop application in Delphi XE4 using TChromium component to display Google Voice webpage.
I need to start a call programmatically and I can't find a way to trigger the javascript code behind the button (it is a DIV) "CONNECT":
TChromium allows to execute javascript code and I already managed to simulate the click on "CALL" button using a javascript code that simulates the Key Event using the character "c" which display the panel. This works because Google Voice has shortcuts and "c" is a valid shortcut to start a call. Also with javascript I can set the number in the input field. My main problem is that I don't know how to simulate a click on "CONNECT":
As you can see, there is no ID, no onClick and there is no shortcut to trigger the Connect button.
I can get a reference to this DIV using the following code:
document.getElementById(":87.mi").children[0].children[0].children[5].children[1].children[0].children[0].children[0].children[1];
But it is not possible to trigger anything appending .click(). I assume it is because click() will trigger onClick method that it is not defined. Is there any way to know what javascript code executes when someone clicks over this div?
If the focus is in the "Number to call" input, I can press "TAB" to navigate to "Phone to call with", then to "Remember to choice" and finally to "Connect" where I can press Enter key to make it work also. I tried to use the same code as in the beginning to simulate the CALL button but this time with TAB (keycode 9 instead of 67) and it does not work as the focus does not move.
I tried to do it programmatically with delphi also using mouse_event, keybd_event, PostKeyExHWND, PostKeyEx32 and PostMessage with no results as the focus does not move away from "Number to call"
The only option that works by now is to move the mouse with delphi using SetCursorPos and simulate a click on that button calculating the coordinates but this will be my last option to choose as I would prefer a javascript code to do it.
Any suggestions are welcome!
I suggest the next solution.
In a javascript file that have to be imported from the view, put the next code:
$(".goog-button-base-content").click(function(){
//CODE HERE
});
What you do here is that everytime that we pressed the div that has that class, you will start the logic inside "CODE HERE"
(Sorry for my bad english)
I hope be usefully. See you

Categories

Resources