MasterACE14 Posted October 19, 2008 Share Posted October 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129045-not-running-php-its-displaying-html-source/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 19, 2008 Share Posted October 19, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/129045-not-running-php-its-displaying-html-source/#findComment-668978 Share on other sites More sharing options...
MasterACE14 Posted October 19, 2008 Author Share Posted October 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/129045-not-running-php-its-displaying-html-source/#findComment-668981 Share on other sites More sharing options...
corbin Posted October 19, 2008 Share Posted October 19, 2008 Make bet a div or something and change the content with innerHTML. Quote Link to comment https://forums.phpfreaks.com/topic/129045-not-running-php-its-displaying-html-source/#findComment-669367 Share on other sites More sharing options...
MasterACE14 Posted October 21, 2008 Author Share Posted October 21, 2008 Make bet a div or something and change the content with innerHTML. I understand the div part, but what do you mean by innerHTML? Quote Link to comment https://forums.phpfreaks.com/topic/129045-not-running-php-its-displaying-html-source/#findComment-670764 Share on other sites More sharing options...
JasonLewis Posted October 21, 2008 Share Posted October 21, 2008 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)" /> Quote Link to comment https://forums.phpfreaks.com/topic/129045-not-running-php-its-displaying-html-source/#findComment-670770 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.