简体   繁体   English

如何使用PHP从Javascript到MySQL数据库获取信息?

[英]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: 以下是有关代码的几件事:

  • The person that created the online surveys had the online surveys written in Javascript (saved as HTML files) 创建在线调查的人使用Javascript(保存为HTML文件)编写了在线调查。
  • Each survey is written in a separate file 每个调查都写在一个单独的文件中
  • Each survey has multiple data to be stored 每个调查都有多个数据要存储
  • Every time the user hits the next button, it goes to another page of the survey 每次用户单击下一个按钮时,它将转到调查的另一页

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: 我不了解/需要帮助的地方:

  1. How do I make this so that when the user hits the "Next" button, it not only goes to the next page (what it's doing right now), but also sends the info to be stored in the database? 我如何做到这一点,以便当用户单击“下一步”按钮时,它不仅转到下一页(当前正在做什么),还发送要存储在数据库中的信息?
  2. These surveys should be filled by people on their own computers so the surveys are written in JS (client-side). 这些调查应由人们在自己的计算机上填写,因此调查以JS(客户端)编写。 The storing part should be written in PHP (server-side) and MySQL, correct? 存储部分应该用PHP(服务器端)和MySQL编写,对吗? Does this mean that I have to create a separate PHP file to create the code for transferring the data to the database or can it all be done in the same file? 这是否意味着我必须创建一个单独的PHP文件来创建用于将数据传输到数据库的代码,还是可以全部在同一文件中完成? (I would think that I would need to create a separate file, one for each survey.) (我认为我需要创建一个单独的文件,每个调查一个。)

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!! 先感谢您!!

  1. 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. } } } 
  2. 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.

相关问题 如何从 MySQL 数据库获取 php 日期到 javascript? - How to get php date from MySQL database to javascript? 如何使用 JAVASCRIPT 从 Input 标签中获取值,输入标签的值是使用 php 从 mysql 数据库中获取的 - How to get values from Input tag using JAVASCRIPT ,the value of the input tags are fetched from mysql database using php Rails-如何从数据库获取信息并将其放入javascript var - Rails - how to get information from database and put into javascript var 如何从 php 和 javascript 的数据库中获取价格 - how to get price from database in php and javascript 将数据库信息从php传输到javascript-mysql_fetch_array无法正常工作 - Transmitting database information from php to javascript - mysql_fetch_array not acting as expected 使用php / javascript从mysql数据库在google map上显示标记 - Displaying markers on google map from mysql database using php/javascript 如何使用更新的mysql数据库信息更新javascript中的php变量? - How do I update my php variables in javascript with updated mysql database information? 如何在javascript函数中使用php的mysql数据库中的值 - how to use the values from mysql database from php in javascript function 如何使用 AJAX 和 PHP 将 JavaScript 变量发布到 MySQL 数据库中? - How to post a JavaScript variable into a MySQL database using AJAX and PHP? 如何使用 ZE1BFD762321E409639CEEFFEE780E2F919Z function 将 mysql 数据库数据传递给 javascript function? - How to pass a mysql database data to a javascript function using php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM