Good day,
am trying to use javascript to pop out two modal page on my hotspot login page: eg
i have tried this:
<?php
$mac=$_POST['mac'];
$ip=$_POST['ip'];
?>
<button onclick="connect()" type="button" class="btn btn-info">CONNECT</button>
<script type="text/javascript">
function connect()
{
$('#login').modal();
}
</script>
the above code show the login modal when the CONNECT button is clicked but i want if the ip and mac address is not present when the clients click CONNECT button Error Modal Should Pop up instead of login modal.
thanks in advace.
You'll have to echo the values from PHP to Javascript. But beware that you need to validate these values first.
Something like this should work:
<?php
// get values from $_POST or default to empty string
$mac = $_POST['mac'] ?? '';
$ip = $_POST['ip'] ?? '';
?>
<button onclick="connect()" type="button" class="btn btn-info">CONNECT</button>
<script type="text/javascript">
function connect() {
// pass values as string to javascript variables
var mac = "<?php echo $mac; ?>";
var ip = "<?php echo $ip; ?>";
// mac and ip adresses provided
if (mac && ip) {
$("#login").modal();
}
// mac or ip adresses missing
else {
$("#error").modal();
}
}
</script>
A few things to note:
This is not secure as I values passed as POST can be tampered with. As a rule of thumb, do not trust anything provided by the client.
<?php echo... ?>
will "print" the values in the document. If you inspect the code in the browser, you'll see something like:
var mac = "mac-address-passed-as-POST"; var ip = "123.456.78.90";
You can simplify the echo
statements by using the shorthand syntax:
var mac = "<?= $mac?>"; var ip = "<?= $ip?>";
To better validate the values you get in the $_POST
variable, you could add regex checks in the PHP part. Something like:
<?php // default values $mac = ''; $ip = ''; // validate mac address if (isset($_POST['mac']) && preg_match('/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/', $_POST['mac'])) { $mac = $_POST['mac']; } // validate ip address if (isset($_POST['ip']) && preg_match('/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/', $_POST['ip'])) { $ip = $_POST['ip']; }?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.