<form action="" method="post" name="theform">
<table width="693" border="1" style="table-layout:fixed;">
<tr>
<td width="683" id="mymessage" contenteditable="true" name="mymessage">Write message here...</td>
</tr>
</table>
<script>
document.getElementById("mymessage").addEventListener("click", removePlace);
function removePlace()
{
document.getElementById("mymessage").innerHTML="";
}
</script>
<div id="google_translate_element"><span class="notranslate">Select language to translate your text above:</span></div>
<script type="text/javascript">
function googleTranslateElementInit()
{
new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,fr,it,ja,ko,ms,ru,ta,th,zh-CN', layout: google.translate.TranslateElement.InlineLayout.SIMPLE, multilanguagePage: true}, 'google_translate_element');
}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<input type="hidden" id='hiddenInput' name='hiddenInput'>
<span class="notranslate"><input type="submit" id="btnSend" name="btnSend" value="Send"></span>
</form>
<script type="text/javascript">
document.getElementById("btnSend").onclick = function() {
var tmsg = document.getElementById("mymessage").text;
var msg = document.getElementById("mymessage").innerHTML;
var tmsg = msg;
var newmsg = tmsg.replace(/<font>/g, "");
var newmsg1 = newmsg.replace("</font>", "");
var newmsg2 = newmsg1.replace("</font>", "");
var newmsg3 = newmsg2.replace("</font>", "");
var newmsg4 = newmsg3.replace("</font>", "");
var newmsg5 = newmsg4.replace("</font>", "");
var newmsg6 = newmsg5.replace("</font>", "");
var newmsg7 = newmsg6.replace("</font>", "");
var newmsg8 = newmsg7.replace("</font>", "");
var newmsg9 = newmsg8.replace("</font>", "");
var newmsg10 = newmsg9.replace("</font>", "");
var newmsg11 = newmsg10.replace("</font>", "");
var newmsg12 = newmsg11.replace("</font>", "");
var newmsg13 = newmsg12.replace("</font>", "");
var newmsg14 = newmsg13.replace("</font>", "");
var newmsg15 = newmsg14.replace("</font>", "");
};
</script>
<?php
$servername = "localhost";
$username = "mytranslateim";
$password = "qwerty";
$dbname = "test";
$dbconnectivity = mysqli_connect($servername, $username, $password, $dbname);
if (isset($_POST['btnSend']))
{
$getmsg = newmsg15;
if($getmsg == "")
{
echo "nothing";
}
else
{
echo $getmsg;
$sql = "INSERT INTO testing(testmsg) VALUES ('$getmsg')";//if i translated a text, for example i translate the word "test" in chinese, it will echo in chinese but will not save in database as chinese
$insertit = mysqli_query($dbconnectivity, $sql);
}
}
?>
如何将javascript变量存储为php变量并将其存储在数据库中?例如,我想将javascript var newmsg15设置为php变量$ getmsg。之后我想将它存储在数据库中。我怎么能这样做?请帮我谢谢。
答案 0 :(得分:0)
如何将javascript变量存储为php变量
是否会为您制作任何场景?
您不需要将javascript变量用作php,但是您需要从服务器端的客户端获取一些数据。
您可以做的是从数组或json中的脚本中收集所需的所有数据,并通过提交表单将其传递给服务器。
在提交表单之前,您需要执行此操作 代码:
for(i=0; i< yourArray.lengt;i++){
var input = document.createElement("INPUT"); // Create a <input> node
input.name="data[]";
input.value=yourArray[i];
document.getElementById("yourform").appendChild(input);
}
这将为您提供服务器端数据的数组。
希望这会对你有所帮助。