[英]How to get information from Javascript to MySQL database using PHP?
( NEWBIE ALERT ) ( NEWBIE ALERT )
Hello! 你好! I am working on developing a database that stores information that people enter onto these online surveys.
我正在开发一个数据库,用于存储人们输入到这些在线调查中的信息。 I don't have experience with Javascript, but I have worked with PHP and MySQL before.
我没有使用Javascript的经验,但是我之前曾使用PHP和MySQL。 I am currently stuck on how to store the data to the database.
我目前对如何将数据存储到数据库感到困惑。 Here are a few things about the code:
以下是有关代码的几件事:
I've worked on a project similar to this before, but my forms were only a page, so whenever the user clicks the "Submit" button, I had it go to another webpage written in a separate PHP file (kind of like a "results" page). 我之前从事过与此项目类似的项目,但是我的表单只是一个页面,因此,每当用户单击“提交”按钮时,我都会转到另一个用单独的PHP文件编写的网页(有点像“结果”页面)。
WHAT I DON'T UNDERSTAND/NEED HELP ON: 我不了解/需要帮助的地方:
Here's a general structure of how the HTML file: 这是HTML文件的一般结构:
<!DOCTYPE html> <html> <head><link rel="stylesheet" type="text/css" href="survey.css"> <script> function Q2(){ document.getElementById("Q").innerHTML = "does something...<button type='button' onclick='Q3()'>Next</button>"; function Q3(){ document.getElementById("Q").innerHTML = "does something...<button type='button' onclick='Q4()'>Next</button>"; function Q4(){ document.getElementById("Q").innerHTML = "does something...<button type='button' onclick='Q5()'>Next</button>"; //keeps going until the last question </script> </head> <body> <h1>Meeting 1</h1> <p id="Q">Some text...<br><input type="text" name="tweet" style='height: 50px;width: 500px;'><br><br> <button type="button" onclick="Q2()">Next</button> </p> </body> </html>
I've done a bit of research and looked at a few textbooks. 我做了一些研究,看了几本教科书。 I think AJAX may be something that I need to use?
我认为AJAX可能是我需要使用的东西? But I'm not too sure.
但是我不太确定。 If possible, could someone explain to me what I should be doing?
如果可能,有人可以向我解释我应该怎么做? I would like to not only be able to find a solution for this, but understand it as well.
我不仅希望能够找到解决方案,而且也希望了解它。
Thank you in advance!! 先感谢您!!
For sending data to a PHP page using JavaScript, I'd recommend using the jQuery framework, where you can do it in as simple a code as this: 为了使用JavaScript将数据发送到PHP页面,我建议使用jQuery框架,您可以在其中使用以下简单的代码来完成它:
function Q2(){ var tweet = $("input[name='tweet']").val(); $.post("your_receiving_page.php", { data : tweet }, function(response){ //POST to PHP page where $_POST["data"] is the tweet variable //deal with PHP output here console.log(response); if(response=="success"){ //javascript code to go to next page etc. } } }
That way, you make a PHP file called "your_receiving_page.php" (or whatever) and handle the posted data like so: 这样,您将创建一个名为“ your_receiving_page.php”(或其他名称)的PHP文件,并按如下方式处理发布的数据:
<?php $tweet = $_POST["data"]; //do stuff with $tweet, eg put it in a database //... //then end the code with "success", which is what you're looking for in the JavaScript as a successful callback exit("success");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.