Toggle two PHP files using jQuery click function - javascript

I have two PHP files one called cab.php and another called d3.php , I want to toggle
in the same file this works cab.php
the problem is when I call the second file d3.php
function botaod2mostraesconde()
{
$(".wrapd2").toggle();
$(".datad2").toggle();
$(".uploadd2").toggle();
}
here
the input button :
<input type="button" value="D3" onclick="location.href = 'd3.php'">
I am looking for a way to toggle from different files. Thanks

You will need to run the JavaScript on the next page (d3.php). You can't execute client side script (JavaScript) on a php page that runs server side which hasn't yet been loaded in the client's browser.
This is based on an edict written in stone eons ago:
Thou shalt not execute client side code before server side code.
This would be akin to Luke attempting to destroy the Death Star before it's been created. Wait for the Death Star, you must. Then use The Force to destroy it.
Run d3.php first, then toggle elements on the page using JavaScript.

Related

Call JavaScript function via a remote script

I have a website, this website is running JavaScript scripts.
Now I have another python script that is running on another machine (in another network even) that should call a JS function on that specific website.
Now, how do I go about that?
I was thinking about a URL that directs to that very JS function...
But how do I call it then?
I tryed to search it, never really found anything like that... Only somewhat related, but never how to call it from a different machine in a different network.
Clarification:
I want to trigger the "Delete Comment" button in Steam Groups via python.
The URL is http://steamcommunity.com/groups/[name]#comments and the JS function call is
javascript:CCommentThread.DeleteComment( 'clanID', 'commentID' );
So I found this script:
http://steamcommunity-a.akamaihd.net/public/javascript/forums.js
Any idea how to trigger a function in there?

How to display an animated icon during python function processing in django?

I am trying to develop an application with Django. I have a form in my HTML file which will pass same data to the server. At server side, my python function(called submit) will receive the posted values and process them and then redirect the user to a new page.
Question: How can I show a loading gif to the user until my python function is processing posted data and finished? I searched on the stackoverflow about this question and there are some answers (this and this and this) but the answers are for finishing Ajax functions, or there is no explanation how can I identify when my python function has been finished or is for flask framework.
Any help would be greatly appreciated.
This is rather simple. First you will have to download font-awesome.css from here http://fontawesome.io/.
Then create one element like <span class="fa fa-spinner"></span> this element will be hidden by default. Set it visible at the beginning of your ajax call and hide it once you have receive the response.
If you want to show your own image, the logic should be the same.

Retrieving data from browser to JavaScript

I have just started out working with JS and I've managed to post data from a MySQL db to the website using node.js, jade and plain JS.
Now I'm trying to do the other way around, i.e. getting data from the website, to the JS code and then inserting it into the db.
What I'm thinking is simply making a textfield with a button. When I fill the textfield and press the button it is collected by the JS script and the inserted to the DB.
I am however having problems with Jade and the listener and I'm unable to even do a console.log using the listener.
This is what I've got so far in my .jade file.
extends layout
script.
var something = function() {
console.log('something')
}
block content
button(onclick='something()') Click
The website renders nicely, but nothing is printed when I click the button.
If someone could give a hint on how to fetch the data in my .js file that would also be appreciated.
In the context of the WWW there are two places that JavaScript can run.
On the server, e.g. with node.js
On the browser, embedded in a <script> element
Since you want to put the data into a database on the server, you want to go with option 1. So don't use a <script> element.
Use a <form> (you could use client side JS to read the data from the form and send it to the server (Ajax) but that seems overcomplicated for your needs and should be layered on top of a plain HTML solution if you were to go down that route).
form(action="/myendpoint" method="post")
label
| Data
textarea(name="foo")
button Submit
Then you just need to write server side code to retrieve that. The specifics of that will depend on how you are implementing the HTTP server in Node.
The question How do you extract POST data in Node.js? provides some starting points.

VB.NET calls Javascript function. How do I get javascript function to complete before VB.NET completes?

In a web application I am using a button_Click method in VB.Net to occur when a button is clicked.
I have the following line at the top of my VB.NET method:
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "myFunction", "myFunction();", True)
I want that script to finish before my VB.NET script carries on. Basically, I have validation in my javascript that I want to complete before the VB.NET takes the "validated" data and inserts it into a database.
This betrays a misunderstanding of how web forms work. Here's what really happens:
A user requests your page from their browser for the first time
Your web server runs the ASP.Net page life cycle in order to send an html response
The web server destroys the page class instance it used to complete the request.
The response from the server arrives and is rendered by the user's browser.
The user clicks your button, resulting in a new http request.
The browser destroys the existing html DOM.
The request arrives at the web server, which then runs the full ASP.Net life cycle again, including the Page_Load method.
This time the data included with the request indicates to ASP.Net that it should also run your button's Click code.
The button registers the javascript to run when the page loads in the browser.
The page lifecycle completes, and ASP.Net sends it's HTML response back to the browser.
ASP.Net destroys the page class instance again.
The response arrives at the browser, which renders it from scratch by creating a whole new html DOM.
The page's javascript load event fires, and some javascript included with ASP.Net pages kicks off the javascript startup script registered by the button.
I need to point out some things about this process, namely that order between steps 3 and 4, steps 6 and 7, and steps 11 and 12 are accurate. When there is working page visible in the browser, the server has already moved on and destroyed anything used to create that page (except Session variables). While VB.Net code is running, the browser doesn't even have a page to show yet.
What you should learn from this is that at time the javascript runs, not only has the VB.Net method already finished, but the entire page class was already destroyed. There's an idea of continuity here for both the browser's web page and the VB.Net Page class instance that just doesn't happen. It's just nice that all this happens in a way that is mainly transparent to the user.
Fortunately, there are some things you can do to avoid this full process. You might look into using an UpdatePanel for part of your page, changing the button to trigger a WebMethod, or translating more of the VB.Net code into javascript in the first place. However, all of these will likely require significant re-thinking of how your page is going to work. In this case, you might find and a Validation control best fits your needs.
This is assuming that myFunction is a javascript function existing on your client side. It will call myFunction on the client side.
<asp:Button ID="btntest" runat="server" Text="Add Record"/>
<asp:CustomValidator ID="myCustomValidator" runat="server" ControlToValidate="someControl" ErrorMessage="Validation Error" ClientValidationFunction="myFunction"></asp:CustomValidator>
This is assuming that you javascript is doing some validation as well. It would look something like this. If args.IsValid = false, then the validator won't allow a postback and the vb.net code won't execute. This is the point of a validator.
function myFunction(sender, args) {
var someControl = document.getElementById(sender.controltovalidate).control;
//Let's assume someControl is a textbox and we don't want it bigger than 10
if (someControl.value > 10) {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
Hopefully, this gets you going. Let me know if something isn't working right and you need more help.

Can you use JavaScript to detect a file download window created server side?

I have a jQuery plugin I use to dynamically create and render a form on a default.aspx asp.net page, then submit it. The page it gets submitted to is a pdf.aspx page. The page builds a PDF then uses Response.Write to write the file (application/pdf) to the browser. I use the same method to render XLSX files to the browser as well. It works really great, but I need a callback or some event to tell the button when to stop spinning. This prevents the user from continuously clicking the Excel or PDF buttons. Does anyone know a way to detect the file dialog window when it was not created using JavaScript? I am also open to other methods of callback from the server side as well.
The way I do that was suggested in response to a question I asked here a while ago by T.J. Crowder. I can't find the response from the last time I wrote this up because the Stackoverflow "search" facility is so incredibly lame, so I'll probably type in a blog post. The basic idea is that your client code (Javascript) should append an extra parameter when it submits the request for the download. The parameter should contain some generated random string (probably just the current timestamp is good enough). The server then looks for that parameter, and when it's preparing the response with the download file it also sets a cookie and gives it that random value.
Right after the submit (or right before; it doesn't really matter), the Javascript code should start an interval timer with a routine to look at the value of document.cookie and see if it contains that random string. As soon as the cookie does contain that string, then you know that the server has sent back its response and that the file download dialog has been presented.

Categories