Jump to content

Recommended Posts

I have a form, when I press submit, it doesn't run my PHP code for the form, but instead it displays the HTML source of the page in the text box.

 

my PHP, and Javascript are all in the same file...

 

relevent code only...

<?php
// Double or Nothing
if(isset($action) && $action == "doubleornothing") {
$bet = $_POST['bet'];
if($player['gold'] < $bet) {
	include("top.php");
	die("You do not have that much Copper!");
} elseif(!is_numeric($bet)) {
	include("top.php");
	die("Numbers only!");
} elseif(empty($bet)) {
	include("top.php");
	die("You did not place a bet!");
} elseif($bet < 1) {
	include("top.php");
	die("Positive numbers only!");
}
$try = rand(1,2);
$message_don = "You bet ".number_format($bet)." Copper!<br />";
$message_don .= "Flipping Coin... Double or Nothing...<br />";
	// you won
	if($try == 1) {
		$winnings = $bet * 2;
		mysql_query("UPDATE `UserDetails` SET `gold`=`gold`+'$winnings' WHERE `ID`='" . $player['ID'] . "'") or die("Could not update winnings! ".mysql_error());
		$message_don .= "Double! you win <font color=\"yellow\">".number_format($winnings)."</font> Copper!";
	}
	// you lost
	elseif($try == 2) {
		$losings = $bet;
		if($player['gold'] < $losings) {
			$losings = 0;
		}
		mysql_query("UPDATE `UserDetails` SET `gold`=`gold`-'$losings' WHERE `ID`='" . $player['ID'] . "'") or die("Could not update losings! ".mysql_error());
		$message_don .= "Nothing! you lose <font color=\"red\">".number_format($losings)."</font> Copper!";
	}
}
include "top.php";
echo <<<_AJAX
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your Web Browser does not support AJAX!");
			return false;
		}
	}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		document.doubleornothing.bet.value = ajaxRequest.responseText;
	}
}
ajaxRequest.open("POST", "gamble.php?action=doubleornothing", true);
ajaxRequest.send(null); 
}

//-->
</script>
_AJAX;
?>

<tr><th align="center" colspan="2">Double or Nothing</th></tr>

<?php
if(isset($message_don)) {
echo "<tr><td>";
echo $message_don;
echo "</td></tr>";
}
?>

<tr><td>
<p>
Place your bet!
<form name="doubleornothing" method="post">
<input type="text" name="bet" />
<input type="button" value="Flip the Coin!" onclick="ajaxFunction();" />
</form>
</p>
</tr></td>

 

any help is greatly appreciated!

 

Regards ACE

 

It is doing exactly what the code says to do -

 

document.doubleornothing.bet.value = ajaxRequest.responseText;

 

<input type="text" name="bet" />

 

Anything that is output by the code on that page when it is requested by ajaxRequest.open("POST", "gamble.php?action=doubleornothing", true); and is returned in responseText will be put into the text input field.

 

On a bad value, you are going to get anything that is output by top.php followed by one of the lines in the die() statements. On a good value, you are going to get everything else that that page outputs when it is requested.

 

What exactly do you want it to do?

well without the AJAX part of it, the Double or Nothing betting is working perfectly fine. All I want to do, is make it so you can do the betting like normal, and submit your bet, without having to refresh the page.

You can change the text inside a div by setting its innerHTML to a value.

 

Example:

<script language="javascript">
function changeText(txt){
document.getElementById("testdiv").innerHTML = txt;
}
</script>

<div id="testdiv">Some text here!</div>

<input type="text" name="testinput" id="testinput" onkeyup="javascript:changeText(this.value)" />

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.