Aurelijus Aurelijus - 4 months ago 6
HTML Question

Form method get not working

I don't know why my form get won't work, here is my code:
adding words because can't post edit..............................................
P.S all other piece of code works perfect!!!

Page: 1:

<button id="Head" onclick="rollHead()">Head</button>
<button id="Tail" onclick="rollTail()">Tail</button>

<script>
function rollTail(){
var die1 = document.getElementById("die1");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random() * 2) + 1;
if(d1 == 1)
{
die1.innerHTML = "You won!";
fliped.innerHTML = "Fliped Tail!";
var ajax = new XMLHttpRequest();
ajax.open('POST','won.php',true);
ajax.send();
}
else if (d1 == 2)
{
die1.innerHTML = "You lose!";
fliped.innerHTML = "Fliped Head!";
var ajax = new XMLHttpRequest();
ajax.open('POST','lose.php',true);
ajax.send();
}
}
function rollHead(){
var die1 = document.getElementById("die1");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random() * 2) + 1;
if(d1 == 1)
{
die1.innerHTML = "You lose!";
fliped.innerHTML = "Fliped Tail!";
var ajax = new XMLHttpRequest();
ajax.open('POST','lose.php',true);
ajax.send();
}
else if (d1 == 2)
{
//alert(document.getElementById('wonorlose').value );
die1.innerHTML = "You won!";
fliped.innerHTML = "Fliped Head!";
var ajax = new XMLHttpRequest();
ajax.open('POST','won.php',true);
ajax.send();
}
}
</script>

<form method="get" action="won.php">
<h2 align="center">
<input type="text" name="wonorlose" value=50>
</h2>
</form>


Page: won.php:

<?php
include_once 'dbconnect.php';
session_start();

if(!isset($_SESSION['user']))
{
header("Location: /manopuslapis/index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

$wonorlose = $_GET["wonorlose"];
echo $wonorlose;
mysql_query("UPDATE `users` SET credits=credits+'$wonorlose' WHERE user_id=".$_SESSION['user']);
?>

Answer

You have problem in won.php at this line

$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);

You have $_SESSION['user'] as string.. This must be integer OR you must add quotes:

$res=mysql_query("SELECT * FROM users WHERE user_id='".$_SESSION['user']."'");

Or define $_SESSION["user"] as integer by adding (int) after = where you define it.

UPDATE

Problem solved with teamviewer. User got blank variable $wonorlose because he make AJAX POST without parameters. I changet AJAX POST into AJAX GET with parameter wonorlose and now its working. Final code have only author.