do without jquery

-1

I want to do the following without the need of jquery, this caught me generates a random word from the db and js calls it to the tag p

<div>
<form action="js/js.js" method="post" name="frm">
<input type="button" name="catga" value="new word" class="b" onclick="boton();">
</form>
</div>
<div id="dos"><p id="worda"></p></div> 

the code js which shows me the word on the label p

function boton() {

$(document).ready(function() {

var pal="#worda";

$(pal).load("./php/php.php");

});
return false;
};

php code which chooses a word from the random db

include("conexion.php");
$codigo = mysql_query("SELECT * FROM categoria_a")or die(mysqli_error()); 
mt_srand(time()); 
$max = mysql_num_rows($codigo); 
$rand = mt_rand(1,$max); 
$obtener = mysql_query("SELECT * FROM categoria_a WHERE codigo_A='$rand'"); 
while($ban = mysql_fetch_array($obtener)) { 

echo $ban['palabra_A']; }

is not to use jquery plis people, maybe jquery simplify it but not to be working with a heavy file plis

    
asked by C. Bustos Alvaro 04.07.2018 в 03:49
source

1 answer

1

equivalences:

$(document).ready(function() {
  // código
});

translates to:

 document.addEventListener('DOMContentLoaded', function() {
   // código
 });

the $.load you can do in a couple of ways, the most advisable at present is to use fetch and promises (if such then happens)

Everything together would look something like this:

var boton = function() {
  fetch('https://baconipsum.com/api/?type=all-meat&paras=2&start-with-lorem=1')
    .then(function(response) {
      return response.text();
    })
    .then(function(body) {
      document.querySelector('#worda').innerHTML = body;
    });
};

document.addEventListener('DOMContentLoaded', function() {
   boton();
});
<div>
<form action="js/js.js" method="post" name="frm">
<input type="button" name="catga" value="new word" class="b" onclick="boton();">
</form>
</div>
<div id="dos"><p id="worda"></p></div>

Note: Are you using an on document load within a click function of a button, #worda would be loaded at the start or each time you press the button? in the example I put both, it is called boton() when loading the DOM and then every click (the url brings random text)

    
answered by 04.07.2018 / 05:00
source