同时使用HTML、PHP和Javascript
我是一个新手,试图了解如何使这3个旋转板一起工作。到目前为止,我只使用了html和javascript,但现在我相信我将不得不使用php html页面有一个带有文本输入和图像上传的表单。目标是,在提交图像时,将其上载到我的服务器,并上载我的firebase实时数据库和信息。我的其他所有页面都没有图像上传,只有文本字段,所以我刚刚为onclick事件调用了一个javascript函数addDeal(),用于提交按钮。更新我的firebase数据库很好 现在,如果我理解google告诉我的,为了集成图像上传,我有一个upload.php页面用于我的表单操作,而不是在提交按钮上调用它。我制作了upload.php页面,将我的图像上传到服务器上效果很好。但现在我不知道如何调用javascript函数,然后将数据添加到firebase数据库中 我是否也应该使用upload.php页面在php中进行firebase更新?或者在我的html页面上保留javascript更新,并以某种方式同时进行?这是我的密码 addDigital.html同时使用HTML、PHP和Javascript,javascript,php,Javascript,Php,我是一个新手,试图了解如何使这3个旋转板一起工作。到目前为止,我只使用了html和javascript,但现在我相信我将不得不使用php html页面有一个带有文本输入和图像上传的表单。目标是,在提交图像时,将其上载到我的服务器,并上载我的firebase实时数据库和信息。我的其他所有页面都没有图像上传,只有文本字段,所以我刚刚为onclick事件调用了一个javascript函数addDeal(),用于提交按钮。更新我的firebase数据库很好 现在,如果我理解google告诉我的,为了集成
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<div class="wholeOption">
<label for="descText">Text to display:
<textarea id="descText" name="descText" cols="80" rows="2"></textarea>
</label>
</div>
<div class="wholeOption">
<label for="fileToUpload">Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
</label>
</div>
<div class="processdeals">
<input type="submit" class="submit" value="ADD DIGITAL MATCHUP" name="submit">
</div>
</form>
<div class="results">
<h2>Results: </h2>
<p id="message" class="message"></p>
<p class="errors" id="errors"></p>
</div>
<script src="https://www.gstatic.com/firebasejs/8.2.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.3/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.3/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.3/firebase-analytics.js"></script>
<script>
var firebaseConfig = {
apiKey: "xxx",
authDomain: "xxx",
databaseURL: "xxx",
projectId: "xxx",
storageBucket: "xxx",
messagingSenderId: "xxx",
appId: "xxx",
measurementId: "xxx"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
<script>
firebase.auth().onAuthStateChanged((user) => {
if (user) {
var uid = user.uid;
} else {
// User is signed out
window.location = '../login.html';
}
});
// this is the function I want to ALSO happen when the form is submitted in addition to the upload.php action
function addDeal(){
let good = 1;
document.getElementById("errors").innerHTML = '';
let descText = document.getElementById("descText").value;
let imageUpload = document.getElementById("imageUpload").value;
let postDate=new Date().getTime();
if (descText == ''){
good = 0;
document.getElementById("errors").innerHTML += "Text to display cannot be blank!";
}
if (good){
// firebase.database().ref('digital/' + postDate).set({
// "desc" : descText,
// "imageUpload" : imageUpload,
// "postedDate" : postDate,
// })
// .then(()=>{
// document.getElementById("message").innerHTML = "added successfully!";
// clearDeal();
// })
// .catch((error)=>{
// document.getElementById("message").innerHTML = "Database error " + error;
// })
} else {
document.getElementById("message").innerHTML = "Digital Deal NOT added. See errors below.";
}
}
</script>
</body>
</html>
要显示的文本:
选择要上载的图像:
结果:
var firebaseConfig={
apiKey:“xxx”,
authDomain:“xxx”,
数据库URL:“xxx”,
投影:“xxx”,
storageBucket:“xxx”,
messagingSenderId:“xxx”,
appId:“xxx”,
度量标准:“xxx”
};
//初始化Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
firebase.auth().onAuthStateChanged((用户)=>{
如果(用户){
var uid=user.uid;
}否则{
//用户已注销
window.location='../login.html';
}
});
//除了upload.php操作之外,在提交表单时,我还希望使用这个函数
函数addDeal(){
让good=1;
document.getElementById(“错误”).innerHTML='';
让descText=document.getElementById(“descText”).value;
让imageUpload=document.getElementById(“imageUpload”).value;
让postDate=newdate().getTime();
如果(descText=''){
良好=0;
document.getElementById(“错误”).innerHTML+=“要显示的文本不能为空!”;
}
如果(好){
//firebase.database().ref('digital/'+postDate).set({
//“描述”:描述文本,
//“imageUpload”:imageUpload,
//“postedDate”:postDate,
// })
//.然后(()=>{
//document.getElementById(“message”).innerHTML=“添加成功!”;
//clearDeal();
// })
//.catch((错误)=>{
//document.getElementById(“message”).innerHTML=“数据库错误”+错误;
// })
}否则{
document.getElementById(“message”).innerHTML=“未添加数字交易。请参阅下面的错误。”;
}
}
upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
要了解哪个是哪个,您可以阅读服务器/客户端体系结构、
HTTP
通信层(不是整个协议,而是它的用途),并阅读PHP
如何在这方面帮助您
简言之:
您的浏览器“理解”或“显示”HTML
和CSS
。您可以通过Javascript
操作文档和浏览器。这一切都是在“客户端”进行的,所以在您的浏览器中
如果必须询问“服务器”,浏览器必须与“服务器”通信。此服务器将获取客户端通过HTTP
发送给它的数据,并执行PHP
解释器,解释器反过来执行PHP
脚本
这个PHP
脚本将接受所谓的HTTP
请求,浏览器将该请求发送到服务器(发送到PHP
脚本),并再次通过HTTP
进行应答。答案通常是HTML页面。您可以使用它编写PHP
脚本和HTML
当浏览器从服务器获取答案(响应)时,它将显示一个新页面
正如我所说,PHP
脚本将在服务器端工作,HTML
和Javascript
将在客户端工作
这是您提到的那些技术最常用的用法。要了解哪个是哪个,您可以阅读服务器/客户机体系结构、
HTTP
通信层(不是整个协议,而是它的用途),并阅读PHP
如何在这方面帮助您
简言之:
您的浏览器“理解”或“显示”HTML
和CSS
。您可以通过Javascript
操作文档和浏览器。这一切都是在“客户端”进行的,所以在您的浏览器中
如果必须询问“服务器”,浏览器必须与“服务器”通信。此服务器将获取客户端通过HTTP
发送给它的数据,并执行PHP
解释器,解释器反过来执行PHP
脚本
这个PHP
脚本将接受所谓的HTTP
请求,浏览器将该请求发送到服务器(发送到PHP
脚本),并再次通过HTTP
进行应答。答案通常是HTML页面。您可以使用它编写PHP
脚本和HTML