Help to multi links cURL

hi astonecipher
i rely thx for your code just a littel q You no i am blank to coding and from denmark :slight_smile:)
Can you plz post a complet code?

I have try to put it in a html but it will not work i also try php but samm outcom ?
I no i rely need to read on all these codes :frowning:

i just get this outcom on page:

$(document).ready(function(){ var state = "ON"; $('input[type="button"]').on('click', function(){ $( [1,2,3,4] ).each(function() { var status = state == "ON" ? "Off" : "ON"; $.get( "http://http://192.168.1." + this + "0/led?state=TURN+RED+" + status, function( data ) { $( "#b" + this ).removeClass(status == "ON" ? "active" : "inactive"); $( "#b" + this ).addClass(status == "ON" ? "inactive" : "active" ); }); }); }); }); ?>     .inactive { background-color: grey; color: white; } .active { background-color: red; } 
111
I think i miss somthing ? :)

Cheers
       Jimmy

What html do you currently have?
And you added an extra http:// in the request

it is here and sry for my bad coding and Q :frowning:

Ny side 1 $(document).ready(function(){ var state = "ON"; $('input[type="button"]').on('click', function(){
  $( [1,2,3,4] ).each(function() {
  var status = state == "ON" ? "Off" : "ON";
  $.get( "http://192.168.1." + this + "0/led?state=TURN+RED+" + status, function( data ) {            
            $( "#b" + this ).removeClass(status == "ON" ? "active" : "inactive");
            $( "#b" + this ).addClass(status == "ON" ? "inactive" : "active" );          
   });
  });  
});

});

.inactive {
background-color: grey;
color: white;
}

.active {
background-color: red;
}

hmm how do i post so all code are coming in my post?

So, you are missing the actual HTML page?

yes
i have try my self but i only getting your clean code on the page?

https://www.w3schools.com/html/

and where do i inset your code?

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

in

?

Put all of Astonechiperā€™s code together like this:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<input type="button" class="inactive" id="b1">
<input type="button" class="inactive" id="b2">
<input type="button" class="inactive" id="b3">
<input type="button" class="inactive" id="b4">
<style>
.inactive {
  background-color: grey;
  color: white;
}

.active {
  background-color: red;
}
</style>
<script>
$(document).ready(function(){ var state = "ON"; $('input[type="button"]').on('click', function(){

  $( [1,2,3,4] ).each(function() {
  var status = state == "ON" ? "Off" : "ON";
  $.get( "http://192.168.1." + this + "0/led?state=TURN+RED+" + status, function( data ) {            
            $( "#b" + this ).removeClass(status == "ON" ? "active" : "inactive");
            $( "#b" + this ).addClass(status == "ON" ? "inactive" : "active" );          
   });
  });  
});
</script>

</body>
</html>
1 Like

Hi ErnieAlex and astonecipher
I rely thank you for your time, but still it is not working? :frowning:
on the web page wend i put the code in it look good, i dont no why i have 4 bottons? but wend i press a botton it is not doing anything?
Just so you understand me right.
I can open a web browser insed http:// 192.168.1.10/led?state=TURN+RED+ON (just put a space in so it is not a link) and it will turn my red on on that mcu and http:// 192.168.1.20/led?state=TURN+RED+ON on the orher mcu and so onā€¦ and ofcause http:// 192.168.1.10/led?state=TURN+RED+OFF wil turn it off on thar mcu, no problem.
So what i look for is one botton there activate my red on all my 8 mcuā€™s i cant see how the code can do it?

Sorry you no i an newbe big time :slight_smile:

Cheers
Jimmy

How did you debug it?

cURL READS pages. It also POSTS to pages. Using the Javascript and AJAX version also will read (get) or post. I am guessing that your current code is needs to post the info, not get it. Therefore, you would need to change that like something like this:

  $.post( "http://192.168.1." + this + "0/led", "?state=TURN+RED+" + status, function( data ) {            
            $( "#b" + this ).removeClass(status == "ON" ? "active" : "inactive");
            $( "#b" + this ).addClass(status == "ON" ? "inactive" : "active" );          
   });

Note 100% sure, but, I think this should handle it. Try it and let us know.

EDITED! Perhaps Astonechiper can review. (I am not that good at Javascript/AJAX!

How did you debug it?

cURL READS pages. It also POSTS to pages. Using the Javascript and AJAX version also will read (get) or post. I am guessing that your current code is needs to post the info, not get it. Therefore, you would need to change that like something like this:

  $.post( "http://192.168.1." + this + "0/led", { status: "TURN+RED+" + status, function( data ) }
 {            
            $( "#b" + this ).removeClass(status == "ON" ? "active" : "inactive");
            $( "#b" + this ).addClass(status == "ON" ? "inactive" : "active" );
   });

Note 100% sure, but, I think this should handle it. Try it and let us know.

Letā€™s try a test with one and get it to work first then.

$(document).ready(function(){
    var state = "ON";
    $('input[type="button"]').on('click', function(){

//      $( [1,2,3,4] ).each(function() {
      var status = state == "ON" ? "Off" : "ON";
      alert("Current status is: " + status);
      $.get( "http://192.168.10/led?state=TURN+RED+" + status, function( data ) {            
                $( "#b1").removeClass(status == "ON" ? "active" : "inactive");
                $( "#b1").addClass(status == "ON" ? "inactive" : "active" );   
                state = (state == "ON" ? "Off" : "ON");
       });
//      });  
    });
});

Astonecipher, does ā€œgettingā€ a page still post the options to the page. I would guess the answer is yes.

If a webpage reads options passed to it, getting the page should activate them, correct? Hard for me to guess as I can not see the code on that page. He said the get version is not working, so I figured we need to use the post version. But, I am guessing on this! Hope you can solve it for him.

So among the various types of HTTP verbs, you have POST, GET, PUT, DELETE, PATCH.

GET is what you use when you access a webpage. You can test this in PHP by doing

if($_SERVER['REQUEST_METHOD'] == 'GET')
    echo "You used GET";
else if($_SERVER['REQUEST_METHOD'] == 'POST')
    echo "You used POST";

So, by him accessing the website he is using a get request. The parameters after the ā€˜state=ā€™ designate a get variable,

$_GET['state']

And that is read in somewhere to do something.

So, based on the url I made the following code:

<?php

if($_SERVER['REQUEST_METHOD'] == 'GET') {
echo "<pre>";
print_r($_GET);
echo "</pre>";
}

And when hit with the url http://stonecipherprojects.com/test/led?state=TURN+RED+ON
Outputs the following.

Array
(
    [state] => TURN RED ON
)

When accessed via browser and when hit using an API tool,

<pre>Array
(
    [state] => TURN RED ON
)
</pre>

Okay, so GET does process the options posted. So, he said this did not work on his system.
He did not get any changes in the LEDā€™s.

Hi just for info.
I am on work and Will try work on it again to morrow

Cheers
Jimmy

Hi
I have try it again but like before it is not working.

Then i try:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>
    </style>
    <script>
    function open_all(){
    	var urls = document.getElementById("list_urls").value;
      var urls = urls.split('\n');
      var totalno = urls.length;
      var s;
      for(var i=0;i<totalno;i++){
        s = urls[i];
        if(s){
        	if(s.substr(0,7)!='http://') s = 'http://'+s;
       	 		window.open(s);
      		}
      	}
      	return false;
      }
    </script>
    </body>
    <form method="post" action="">
    <br />
    <textarea name="list_urls" id="list_urls" cols="60" rows="20"></textarea>
    <br /><br />
    <input value="Open URLs" class="submit" type="button" onClick="open_all();"><br /><input type="reset" value="Reset!"><br/></form>
    </html>

wen i put the link in that it works fine but ofcause it will open a new page per line :frowning:

hmm where is the code?

Did you put the code in either quotes or preformatted-text tags?

Sponsor our Newsletter | Privacy Policy | Terms of Service