我有一个网站,其功能是将字幕从一种语言翻译成另一种语言,翻译是使用google widget工具完成的,在图像上用红色标记:https://ibb.co/LCWt6YV (正如您在左侧看到的,它在谷歌小部件工具“选择语言”上显示)
还有一个选项是用户可以将翻译后的字幕文件保存到数据库中,并且用户翻译字幕的语言会自动保存到数据库中,现在很多用户不知道使用此选项,所以他们不要选择语言
来翻译字幕,只需保留 Google 小部件工具上的选项“选择语言”
即可。
我需要一些函数/代码,以便在未选择语言的情况下不会执行任何操作。
如果他没有通过 Google 小部件工具选择语言并尝试将字幕保存到数据库中,则会出现消息“请在保存字幕之前选择语言”。
这个 php 脚本用于将字幕保存到数据库中,现在我应该在这个脚本中添加哪些函数/代码?
$data = $_POST['data'];
$file_name = $_POST['xxx'];
$subtitle = $_POST['xxx'];
$language = $_POST['xxx'];
$author = $_POST['xxx'];
$ip= $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
$ip = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}
$data =json_decode($data, true);
foreach($data as $obj){
$subtitles1->add($obj['start'], $obj['end'],$obj['text']);
$subtitles1->save($file_name);
}
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_file_name_ext = time()."_".$sub."_".$lan.".".$ext;
$new_file_name = time()."_".$sub."_".$lan;
$sql_lan = "SELECT code FROM lan_code WHERE language ='$lan'";
$result_lan = mysqli_query($conn, $sql_lan);
if (mysqli_num_rows($result_lan) > 0) {
while($row = mysqli_fetch_array($result_lan)){
$lan_code = $row['code'];
}
}else{
$lan_code = $lan;
}
$sub_new = $sub.'_'.$lan_code;
$sql = "SELECT * FROM table WHERE name ='$sub_new'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$data1 = "Unable to save subtitle, subtitle already exists with the same language in the database.";
}
else{
$query1 = "INSERT INTO table (xxx , xxx, xxx, xxx, ip)
VALUES ('$sub_new','$authors', '$lan', '$new_file_name', '$ip')";
if(mysqli_query($conn, $query1)){
copy("$file_name", "uploads/$new_file_name_ext");
//new GoodZipArchive('uploads/$new_file_name', 'upload/output_zip_file.zip') ;
$data1 = "Successful, File saved to database.";
$zip = new ZipArchive;
$new_zip = time()."_".$subtitles."_".$languages.".zip";
if ($zip->open("uploadsk/$new_zip", ZipArchive::CREATE) === TRUE)
{
$zip->addFile("uploads/$new_file_name_ext", "$new_file_name_ext");
$zip->close();
}
} else{
$data1 = mysqli_error();
}
}
echo $data1; ?>
最佳答案
如果你想在未选择语言时弹出消息,则需要使用Javascript。
您可以以一种非常简单的方式使用 javascriptalert() 函数,如下所示:
echo '<script type="text/javascript">';
if(empty($language) or !isset($language)){
echo 'alert("please select a language")';
}else{
...add the rest of your code here.
}
echo '</script>';
但这会导致非常难看的输出。如果你想要更漂亮的东西,你可以使用像 sweetAlert 这样的插件。它非常容易使用,而且比简单地“提醒”您的消息要好得多。您可以查看sweetAlert here .
祝你好运!
关于javascript - 未选择语言时如何收到错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59517666/