Printar php results in Divs

Asked

Viewed 119 times

0

<html>

<head>
  <title> chk </title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script>
    function enviar() {
      var lista = $("#lista_id").val();
      var linhaenviar = lista.split("\n");
      var index = 0;
      linhaenviar.forEach(function(value) {

        setTimeout(

          function() {
            $.ajax({
              url: 'config.php',
              type: 'POST',
              async: false,
              dataType: 'html',
              data: "lista=" + value,
              success: function(resultado) {
                $('#oi').html($('#oi').html() + resultado + "<br>");
              }
            })

          }, 10 * index);

        index = index + 1;

      })
    }
  </script>

</head>

<body>
  <center>
    <textarea name="lista" id="lista_id" rows="10" cols="40">
</textarea>
    <br>
    <input type="button" value="testar" onclick="enviar();"></input>
    <br>
    <div id="live" name="live">
      <p></p>
    </div>
    <div id="Die" name="Die">
      <p></p>
    </div>
  </center>
</body>

</html>

//PHP SEPARADO NO CONFIG.PHP

<?php

$checker = $_POST["lista"];

$ip = explode("|", $lista)[0];
$porta = explode("|", $lista)[1];

function GetStr($string, $start, $end){

$str = explode($start, $string);
$str = explode($end, $str[1]);
return $str[0];
}

$login = ''.$ip.' | '.$porta.'';

$ch = curl_init();
$url = "local da requisição";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "_proxy=$ip&_port=$porta");
$resultado = curl_exec($ch);
curl_close($ch);

$name = $resultado;
$valor = GetStr($name, 'Sair',";");

if ( $valor == "Sair" ) {
 echo '<script>$(document).ready(function(){$("#Live").prepend("'.$login.' <br>")});</script>' ;
} else {

    echo '<script>$(document).ready(function(){$("#Die").prepend("'.$login.' <br>")});</script>' ;
}


?>

How do I display my PHP results in a div? Follow my code, I wish each result to be displayed in your div.

if ( $valor = "Sair" ) {
 echo '<script>$("#funcionou").prepend("'.$logout.' <br>");</script>' ;
} else {

    echo '<script>$("#naofuncionou").prepend("'.$logout.' <br>");</script>' ;
}


<div id="funcionou" name="Lives">

</div>

<div id="naofuncionou" name="Dies">

</div>
  • 1

    Wouldn’t it be easier for you to call echo with the desired message within the div where you want to display it?

  • And that structure wouldn’t even work, because the Divs weren’t loaded when the scripts were called.

1 answer

2


First, the operator = is incorrect on the line:

if ( $valor = "Sair" ) {

The right thing would be to use == (PHP comparison operators):

if ( $valor == "Sair" ) {

Updating the response:

In your code you play the return of Ajax inside a div with id="hi", but this div does not exist in your HTML. This div is required for the script coming from Ajax to take effect.

<script>
function enviar() {
      var lista = $("#lista_id").val();
      var linhaenviar = lista.split("\n");
      var index = 0;
      linhaenviar.forEach(function(value) {

        setTimeout(

          function() {
            $.ajax({
              url: 'conecta.php',
              type: 'POST',
              async: false,
              dataType: 'html',
              data: "lista=" + value,
              success: function(resultado) {
                $('#oi').html($('#oi').html() + resultado + "<br>");
              }
            })

          }, 10 * index);

        index = index + 1;

      })
    }
</script>

Correct code in Ajax (in PHP):

if ( $valor == "Sair" ) {
 echo '<script>$("#funcionou").prepend("'.$logout.' <br>");</script>' ;
} else {

    echo '<script>$("#naofuncionou").prepend("'.$logout.' <br>");</script>' ;
}
  • I am using jquery ajax

  • @Brunolazarine Can you explain what is not working? Given your question, the code above works.

  • I need them to be printed in their backups and I haven’t been able to

  • @Brunolazarine If you can post a part of the code where you want to apply it in Ajax, it would help to find the solution.

  • I posted down there, buddy

  • @Brunolazarine You have to put any code inside the <body> or <head>.

  • Ah tah, got it.

  • Takes the $(document).ready(.... Leave it alone: $("#Live").prepend("'.$login.' <br>");.. See if the id of the div is the same on echo. I see that one is "live" and echo is "Live".

  • @Brunolazarine Another thing: You tah playing the result of Ajx inside a div "#hi", but this div does not exist in your code.

  • @Brunolazarine I updated the answer. Take a look.

Show 5 more comments

Browser other questions tagged

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