Jump to content

Update with AJAX


callesson

Recommended Posts

Hello folks!

 

I got the suggestion to use AJAXS for my problem, update sql without refreshing. But im not that into javascript or Ajax right now so I would like some help!

What I have is:

A game were you can fight Player vs Player. I have this submit button that will make the other player loose Health. So if I press it my friend will loose Health, I will display it but my friend need to update before he will be able to see the change. And I want it to change right away.

 

Right now when someone presses the submit(arena.php) they will be redicted to a_temp.php where the sql statement is, then you will be redirected to arena.php.

 

Arena.php

<form action='a_temp.php' method='post'><input type='submit' name='uatt' value='Uatt'/></form>

 

a_temp.php

<?php 
session_start();
if (!isset($_POST['uatt'])) {
header("Location:landskapdone.php");
exit;
}

include('connect.php');


$query = mysql_query("SELECT * FROM chars WHERE user='$_SESSION[user]'");
while($row = mysql_fetch_array($query))
{
$ulevel = $row['Level'];
$uname = $row['Name'];
$uhp = $row['HP'];
$ump = $row['MP'];
$uHpmax = $row['Hpmax'];
$uMpmax  = $row['Mpmax'];
$udamage = $row['Damage'];
$udamagemax = $row['Damagemax'];

$query = mysql_query("SELECT * FROM chars WHERE User='$_SESSION[from]'");
while($row = mysql_fetch_array($query))
{
$olevel = $row['Level'];
$oname = $row['Name'];
$ohp = $row['HP'];
$omp = $row['MP'];
$oHpmax = $row['Hpmax'];
$oMpmax  = $row['Mpmax'];	

$select = mysql_query("SELECT * FROM arena_inv WHERE a_to='$_SESSION[user]' OR a_from='$_SESSION[user]'");
while($row = mysql_fetch_array($select))
{
$acc = $row['acc'];
$from = $row['a_from'];
$to = $row['a_to'];	
if(isset($_POST['uatt']) && $to ==$_SESSION['user'])
{
$num = rand(0,2) ; 
switch ($num) 
{ 
case 0: 
$makedamage = mysql_query("UPDATE chars SET hp=hp -$udamage WHERE user='$_SESSION[from]'");
break; 
case 1: 
$makedamage = mysql_query("UPDATE chars SET hp=hp -$udamagemax WHERE user='$_SESSION[from]'");
break; 
case 2: 
break; 
} 
$takemana = mysql_query("UPDATE chars SET mp=mp -5 WHERE user='$_SESSION[user]'");
header("Location:arena.php");
}
elseif(isset($_POST['uatt']) && $from ==$_SESSION['user'])
{
$num = rand(0,2) ; 
switch ($num) 
{ 
case 0: 
$makedamage = mysql_query("UPDATE chars SET hp=hp -$udamage WHERE user='$_SESSION[to]'");
break; 
case 1: 
$makedamage = mysql_query("UPDATE chars SET hp=hp -$udamagemax WHERE user='$_SESSION[to]'");
break; 
case 2: 
break; 
} 
$takemana = mysql_query("UPDATE chars SET mp=mp -5 WHERE user='$_SESSION[user]'");
header("Location:arena.php");
}
if($ohp<=0)
{
$honor= mysql_query("UPDATE chars SET Honor=Honor +1 WHERE user='$_SESSION[user]'");
header("Location:Youwon.php");
}
if($uhp<=0)
{
header("Location:Youlost.php");
}
}	
}
}	
?>

I guess i dont need redirect when its dynamic ?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.