Changing links with Javascript or PHP

Asked

Viewed 89 times

0

I have a problem with a button where I need to change its link with javascript or php.

Come on!

My platform generates an automatic form, but I don’t have access to the source code. The form collects name and email and sends to the platform, for this reason the form is already ready. However when collecting the data, continues on the same page, I need to redirect to another page to the user click the button and the button also continue with the functionality of registering the data on the platform, however I do not have the source code as said above.

I can see the id’s, class, name and everything else by "inspecting" the browser, I wonder if there is any way to redirect the page with js or php knowing that I know the attributes above.

<div class="pages">
   <cxc.newsletter/>
</div>

This div is to call the platform form already ready. When it is displayed in the browser, it shows the form with the fields to fill in, and these fields have their id, name, class that I also use to style inputs and button.

  • Please, edit your question and provide a minimal, complete and verifiable example.

  • How do you intend to do this without access to the source code?

  • I have access to all html, less what the platform generates, which is what is inside the div that is in the question.

  • This <cxc.newsletter> calls on the platform the inputs and the button, already with the defined class, id, and default name... has no way to change, I want to know if with this information I have if I can change the button link.

  • Have you tried using the addEventListener javascript?

  • When you submit the data for registration, the page reloads or is an Ajax?

  • What you can not change is the destination of the form, otherwise you will not register the data sent. However, there is a way to redirect after sending the form. You need to know what changes on the page after this submission and knowing this, with Javascript.

  • I don’t know the addeventlistener no Anderson :/

  • Actually it’s not a form, I was trying to make another method and I realized now! It’s just two text inputs and a button just inside that div.

  • It doesn’t matter if it’s a form. You have to know how this works, how data is sent, whether the page reloads etc. This information is crucial for a solution.

  • The page does not reload. And the data is sent by a js code via POST.

  • Got it. But then a confirmation message appears?

  • Yes, and if one of the fields has not been filled in it shows the error message.

  • All right. What we need to know is when the POST ended. With the "inspect element" of the browser, see if the confirmation message has an ID.

  • Yes, id, class tbm

  • Swap the answer ids with the corresponding ids and see if it works.

  • I still don’t understand how you will use the data if you don’t have access, you have html but you don’t have the login form?

  • It’s not a @Wmomesso login, it’s two fields to "capture" data and the send button. I own the html code, but I don’t have the code that is generated by the platform, which is the div I demonstrated in the question.

Show 13 more comments

1 answer

0


Create a addEventListener to the button, and when it is clicked, it will start a setInterval that will be checking if the id of div confirmation exists on the page. When it exists, it will redirect to the page on location.href:

<script>
window.onload = function(){
    document.getElementsByClassName("dados")[0].addEventListener("click", function(){
        temporizador = setInterval(function(){
            if(document.getElementById("id_da_div_de_confirmação")){
                clearInterval(temporizador);
                location.href = "página_para_aonde_quer_ir.html";
            }
        }, 10);
    });
}
</script>
  • Dude I understood what you wanted to do, I thought it would work but I noticed that the class id changes as the page is updated. <div class="dados" id="NewsLetter_24c11322_25e5_49e7_8c80_3803db2a968c"> Could it be that in place of getElementById I cannot use getElementsByClassName

  • @Guilhermeluis Yes, but you have to see if there are other elements with the same class

  • @Guilhermeluis Open the console and boot document.getElementsByClassName("dados").length... if the result is 1, then you can change

  • @Guilhermeluis I will update the answer.

  • Guy caught it, but he’s redirecting when the fields aren’t filled, what can I do to fix it ?

  • @Guilhermeluis This should not happen. You should only redirect when the confirmation of sending the data appears.

  • @Guilhermeluis So the confirmation div already exists before sending the data, and must be hidden. You’d have to find it in the source code and see how it looks, if it’s display: None or something like that.

  • Dude, my code looks like this: <script>&#xA;window.onload = function(){&#xA; document.getElementById("newsletterButtonOK").addEventListener("click", function(){&#xA; temporizador = setInterval(function(){&#xA; if(document.getElementsByClassName("success")){ &#xA; clearInterval(temporizador);&#xA; location.href = "http://www.site.com.br/pagina";&#xA; } &#xA; }, 10);&#xA; });&#xA;}&#xA;</script>

  • @Guilhermeluis Troque o if(document.getElementsByClassName("success")) for if(document.getElementsByClassName("success").length > 0)

  • I switched and you keep redirecting without filling in, which I find strange is that with the document.getElementsByClassName("succes").length or error. It is returning me 1 if I have in Success or if I have in error and try document.getElementsByClassName("error").length He returns me 1, returns me 0.

  • When I do if(document.getElementsByClassName("success").length > 0) in the console, returns me true however when I went to see the source code, the higher sign does not appear, I was replaced by different characters, you know what might be ? It’s like this in the source code if(document.getElementsByClassName("success").length&gt;0)

  • @Guilhermeluis Do the following: before sending the form, open the console (F12) and put this: document.getElementsByClassName("success").length and notes the value that returns... do the same after sending the form and notes what returns, and then tells me the values.

  • Before sending = 0, after sending = 1

  • @Guilhermeluis So that’s right. The problem must be what you said, to replace > by &gt;

  • Yeah, I think that should be it too. You know how I can fix ?

  • @Guilhermeluis Altera para: if(document.getElementsByClassName("success")[0])

  • Dude, I did it with if(document.getElementsByClassName("success").length == 1) and it worked. Thank you so much for your help and understanding. I will mark your response as correct. Vlw itself :)

  • @Guilhermeluis Ah is even, so tb works. Abs!

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.