admin管理员组文章数量:1303389
I am developing a simple web application. I have a MySQL database which stores a value. When I load my website, I want to run a php script that retrieves said value from the DB and passes it to javascript. I then use the javascript to either disable a button or not.
In my index.html:
<form action="someOtherScript.php" method="get">
<input type="submit" name="button1" value="Run me now!">
</form>
<script>
var value = "<?php echo $someVar; ?>";
if(value == 0){
document.getElementsByName("button1")[0].disabled = true;
}
</script>
In my getValueFromDB.php:
<?php
<!-- retrieve value from DB here (This part is not the problem)-->
$someVar = 0;
?>
How can I tell my javascript in the index.html which php script to use (I have multiple)? So when the website loads I want my javascript to get the result from the getValueFromDB.php script.
Thanks a lot!
I am developing a simple web application. I have a MySQL database which stores a value. When I load my website, I want to run a php script that retrieves said value from the DB and passes it to javascript. I then use the javascript to either disable a button or not.
In my index.html:
<form action="someOtherScript.php" method="get">
<input type="submit" name="button1" value="Run me now!">
</form>
<script>
var value = "<?php echo $someVar; ?>";
if(value == 0){
document.getElementsByName("button1")[0].disabled = true;
}
</script>
In my getValueFromDB.php:
<?php
<!-- retrieve value from DB here (This part is not the problem)-->
$someVar = 0;
?>
How can I tell my javascript in the index.html which php script to use (I have multiple)? So when the website loads I want my javascript to get the result from the getValueFromDB.php script.
Thanks a lot!
Share asked Apr 5, 2018 at 13:55 Wolfgang DeigeleWolfgang Deigele 851 gold badge1 silver badge8 bronze badges 7-
you're getting undefined notices here. You need to
.get()
the url for it. – Funk Forty Niner Commented Apr 5, 2018 at 13:57 - 2 Try using an Ajax call – Rotimi Commented Apr 5, 2018 at 13:59
-
2
What about
ajax
? – ArmKh Commented Apr 5, 2018 at 14:00 - I would probably set up an endpoint in PHP that just returns the data (ie echo it as plain text) and make an ajax call to it. – Quangdao Nguyen Commented Apr 5, 2018 at 14:00
-
Include
the desired script before you echo the value – Michel Commented Apr 5, 2018 at 14:00
3 Answers
Reset to default 5Try using the <?php include 'filepath' ; ?>
includes reading documentation here, I think that is what you need
<form action="someOtherScript.php" method="get">
<input type="submit" name="button1" value="Run me now!">
</form>
<script>
<?php include 'getValueFromDB.php' ; ?>
var value = "<?php echo $someVar; ?>";
if(value == 0){
document.getElementsByName("button1")[0].disabled = true;
}
</script>
You can not tell javascript how to use PHP, because JS is a client language and a PHP server language and the workflow is first PHP and second JS and not vice versa.
If you need to take php data with JS, you need to use AJAX
well (it's an example, not tested)
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="someOtherScript.php" method="get">
<input type="submit" name="button1" value="Run me now!">
</form>
<script>
$.ajax({
url: "getValueFromDB.php",
success: function(result){
if(result == 0){
document.getElementsByName("button1")[0].disabled = true;
}
}});
</script>
php
<?php
<!-- retrieve value from DB here (This part is not the problem)-->
$someVar = 0;
echo $someVar
?>
You can try it with ajax, on page load
$( document ).ready(function() {
$.ajax({
url: "yourphpfile.php",
success: function(response){
if(result == 0)
{
document.getElementsByName("button1")[0].disabled = true; // instead of this maybe its better to use jquery - example below
$('#button1').prop( "disabled", true );
}
}
});
});
You would generally have your php script echo the values you want to get and preform an ajax call from your javascript to fetch the values, so
php:
<?php
<!-- retrieve value from DB here (This part is not the problem)-->
$someVar = 0;
echo $someVar;
?>
And js(you can put it directly in the html if you want).
<script>
var dbData;
var ajax = new XMLHttpRequest();
ajax.open("GET", "PATH_TO_PHP_SCRIPT/getValueFromDB.php", true);
ajax.send();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {
var data = ajax.responseText;
dbData=data;
//Deal with the response
}
</script>
or, using jQuery:
<script>
var dbData;
$.ajax({
url: "PATH_TO_YOUR_PHP_SCRIPT/getValueFromDB.php",
method: "GET"
}).done(function(d) {
dbData=d;
//Deal with the response
});
</script>
本文标签: How do I connect javascript and MySQL via PHPStack Overflow
版权声明:本文标题:How do I connect javascript and MySQL via PHP? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741703310a2393418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论