通过一个onClick javascript函数更新使用PHP MySQL数据库 [英] Updating a MySql database using PHP via an onClick javascript function

查看:176
本文介绍了通过一个onClick javascript函数更新使用PHP MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个网页游戏,学习新单词瞄准的孩子。

我有一组四个环节的每个显示从我的数据库和一个线索中检索特定单词,我需要检查已选择的单词为线索相匹配的正确的单词。

我知道我需要使用,因为onClick的功能的JavaScript,我可以成功地检查所选择的单词是否正确的单词相匹配。不过,我则需要更新保存在数据库中,如果这个词是正确匹配得分,因此我需要使用PHP。

从我所知,这意味着我必须使用AJAX,但我无法找到使用链接的AJAX的onClick来再更新数据库的人一个很好的例子。

我试图做到这一点...但我不能让它正常工作,可能是完全错误的:

  //这是我的链接,我需要在我的game.php文件中使用,其中$ newarray [0]是这个问题的答案我要对证$ newarray [$ rand_keys]

&其中;一个的onClick = originalUpdateScore('$ newarray [0]','$ newarray [$ rand_keys]')> $ newarray [0]其中; / a取代;
 

//我尝试在阿贾克斯的score.js文件

  VAR XMLHTTP;

功能originalUpdateScore(OBJ,科尔){
    XMLHTTP = GetXmlHttpObject();
如果(XMLHTTP == NULL)
{
警报(浏览器不支持HTTP请求);
	返回;
}

如果(更正== OBJ){

VAR URL =getscore.php;
// URL = URL +Q =?+ STR;
// URL = URL +&放大器; SID =+的Math.random();
xmlHttp.onreadystatechange = stateChanged;
//xmlHttp.open("GET",url,true);
xmlHttp.open(URL,真正的);
xmlHttp.send(空);

警报(正确);

}
其他
{
警报('AHHHHH!');
}

window.location.reload(真正的);
 

}

 函数stateChanged()
    {
如果(xmlHttp.readyState == 4 || xmlHttp.readyState ==完成)
{
的document.getElementById(txtHint)的innerHTML = xmlHttp.responseText。
}
}
功能GetXmlHttpObject()
{
VAR XMLHTTP = NULL;
	尝试
{
// Firefox,歌剧8.0+,Safari浏览器
XMLHTTP =新XMLHtt prequest();
}
赶上(E)
{
		//IE浏览器
		尝试
{
XMLHTTP =新的ActiveXObject(MSXML2.XMLHTTP);
}
赶上(E)
{
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
}
返回XMLHTTP;
 

}

  //尝试更新一个getscore.php文件数据库

< PHP
// $ Q = $ _ GET [Q];
包括(dbstuff.inc);
$ CON = mysqli_connect($主机,$用户,$ passwd文件,$ DBNAME)
或死亡(查询去世:连接);

$ SQL =更新`temp` SET`tempScore` =`tempScore` + 1 WHERE(temp.Username ='$ _ SESSION [登录名字]');

$ showsql =SELECT`tempScore`从`temp` WHERE(temp.Username ='$ _ SESSION [登录名字]');
$结果= mysqli_query($ CON,$ showsql);

回声$结果;

mysqli_close($ CON);
?>
 

解决方案

我会强烈建议适当学习AJAX - 它不会把你的年龄,但会帮助你了解你,不能用它做什么

这是通过AJAX网页更新一个数据库是很常见的。我会建议使用 jQuery的(JavaScript库)简化您的JavaScript开发。这是一个很好的介绍jQuery和AJAX <一href="http://www.developertutorials.com/tutorials/ajax/getting-started-with-ajax-in-jquery-8-05-11/page1.html">here.

基本上,jQuery将要做的就是写很多的样板code为您服务。什么你最终会写是这样的:

 函数updateScore(答案,正确的){
  如果(答案==正确的){
    .post的$('updatescore.php');
  }
}

...

&LT;一的onclick =updateScore(这一点,正确的)...&GT; &所述; / a取代;
 

你在做什么在这里被发送POST请求updatescore.php时,答案是正确的。

然后,在你updatescore.php,你只需要有PHP code像你已经做的,这将在数据库中更新了比分。

您可以明显地做很多比这更复杂的事情,但我希望这将是足以让你开始。

I am creating a web game for learning new words aimed at children.

I have a set of four links each displaying a specific word retrieved from my database and a clue, I need to check that the word which has been selected matches the correct word for that clue.

I know that I need to use javascript because of the onClick function and I can successfully check whether the word selected matches the correct word. However, I then need to update a score held in the database if the word is matched correctly, therefore I would need to use php.

From what I can gather this means I must use AJAX but I can't find a good example of anyone using AJAX onClick of a link to then update a database.

I have attempted to do this...but its probably completely wrong as I couldn't get it to work properly:

//This is my link that I need to use in my game.php file where $newarray[0] is that answer I want to check against $newarray[$rand_keys]

<a onClick=originalUpdateScore('$newarray[0]','$newarray[$rand_keys]')>$newarray[0]</a>

//my attempt at ajax in a score.js file

var xmlHttp;

function originalUpdateScore(obj,corr){
    xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
	alert ("Browser does not support HTTP Request");
	return;
}

if(corr == obj){

var url="getscore.php";
//url=url+"?q="+str;
//url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
//xmlHttp.open("GET",url,true);
xmlHttp.open(url,true);
xmlHttp.send(null);

	alert('Correct');

}
else
{
	alert('AHHHHH!!!');
}

window.location.reload(true);

}

function stateChanged() 
    { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
	} 
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
return xmlHttp;

}

//attempting to update the database in a getscore.php file

<?php
//$q=$_GET["q"];
include("dbstuff.inc");
$con = mysqli_connect($host, $user, $passwd, $dbname)
or die ("Query died: connection");

$sql= "UPDATE `temp` SET `tempScore`= `tempScore`+1 WHERE (temp.Username='$_SESSION[logname]')";

$showsql = "SELECT `tempScore` FROM `temp` WHERE (temp.Username='$_SESSION[logname]')";
$result = mysqli_query($con, $showsql);

echo "$result";

mysqli_close($con);
?>

解决方案

I would highly recommend learning AJAX properly - it won't take you ages but will help you understand what you can and can't do with it.

Updating a DB from a web page via AJAX is very common. I would suggest simplifying your JavaScript development using jQuery (a JavaScript library). There is a good introduction to jQuery and AJAX here.

Basically what jQuery will do is write a lot of the boilerplate code for you. What you will end up writing is something like this:

function updateScore(answer, correct) {
  if (answer == correct) {
    $.post('updatescore.php');
  }
}

...

<a onclick="updateScore(this, correct)" ...> </a>

What you're doing here is sending a POST request to updatescore.php when the answer is correct.

Then, in your updatescore.php, you just need to have PHP code like you already do which will update the score in the database.

You can obviously do many more complicate things than this, but hopefully that will be enough to get you started.

这篇关于通过一个onClick javascript函数更新使用PHP MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆