Last active
June 23, 2017 04:29
-
-
Save jjcodes78/2063948cad690f96e3bafadb33768e9b to your computer and use it in GitHub Desktop.
Dinamicamente envia um valor via POST para o PHP renderizar HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" | |
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<?php | |
$val = isset($_POST['val']) ? $_POST['val'] : null; // aqui vai obter o valor do campo val do javascript | |
if ($val) { | |
if ($val == "a") { | |
var_dump($val); | |
echo "<script> alert('pressionou ok!')</script>"; | |
} | |
if ($val == "b") { | |
var_dump($val); | |
echo "<script> alert('pressionou cancel!')</script>"; | |
} | |
} | |
?> | |
<script language="javascript" type="text/javascript"> | |
var decisao = confirm("Clique em um botão!"); | |
var myform = document.createElement('FORM'); | |
myform.method = 'POST'; | |
myform.action = '/index.php'; | |
const valueToSend = document.createElement('INPUT'); | |
valueToSend.type = 'HIDDEN'; | |
valueToSend.name = 'val'; | |
if (decisao){ | |
valueToSend.value = "a"; | |
} else { | |
valueToSend.value = "b"; | |
} | |
myform.appendChild(valueToSend); | |
document.body.appendChild(myform); | |
myform.submit(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment