2013-07-06 2 views
-1

페이지를 새로 고치지 않고 PHP 코드에서 결과를 다시 뱉어 내고 싶지만 페이지로 리디렉션 중입니다. 모든 것은 나에게 잘 보이지만 나는 분명히 뭔가를 놓치고있다.

HTML 코드 :

<html> 
    <body> 
    <script src="http://code.jquery.com/jquery-1.10.1.min.js" type="text/javascript"> </script> 
    <script src="my_script.js" type="text/javascript"></script> 
     <form action="bet_process.php" method="POST" id="myForm"> 
    <h2 class="title">Betting Settings<span class="line"></span></h2> 
    <div class="form-box"> 
<label for="bet">Bet Amount</label> 
    <input type="text" name="bet" id="bet" class="text" placeholder="Amount"> 
</div> 
<div class="form-box"> 
<label for="pay">Multiplier </label> 
<input type="text" class="text" name="pay" id="pay"> 
</div> 
<div class="form-box last"> 
<label for="profit">Profit </label> 
<input type="text" name="profit" id="profit" class="text" placeholder="Profit"> 
    </div><!-- End Box --> 
<div class="clearfix"></div> 
<div class="form-box"> 
<label for="chance">Win Chance (%)</label><input type="text" name="chance" id="chance" class="text" value="50" placeholder="Win % - 50.5% Default"> 
</div> 

      <p>Slide to choose win chance or enter it in the input!</p><br><input type="range" id="chanceslider" class="vHorizon" step="0.01" min="0.01" max="98" style="background-color: #00aec8; width: 50%;"> 
</div> 





<div class="form-box"> 
    <input type="submit" name="start1" class="button medium color" value="Roll Dice" id="sub"> 
    </div><!-- End Box --> 
<span id="result"></span> 
</div> 


</form> 
</body> 
</html> 

PHP 코드 :

<?php 

require 'db.php'; 
$uid = $_SESSION['uid']; 
$rand = rand(100, 10000)/100; 
$select_gg_amt = mysql_query("SELECT * FROM `users` WHERE `username` = '$uid'"); 
$select_gg_row = mysql_fetch_array($select_gg_amt); 
$balance = $select_gg_row['balance']; 
$amount1 = $_POST['profit']; 
$amount2 = $_POST['bet']; 
$time_ago = date("F j, Y, g:i a"); 
if(isset($_POST['start1'])) { 

if(isset($_POST['bet'], $_POST['pay'], $_POST['profit'], $_POST['chance'])) { 

    if($balance > 0 && $_POST['bet'] > 0) { 

     if($_POST['bet'] <= $balance) { 

    if($rand < $_POST['chance']) { 

echo '<h3>You rolled a <strong>' .$rand. ' </strong> out of 100 on the percentile dice! You won!</h3>'; 
$result = 'Win'; 

mysql_query("UPDATE `users` SET `balance` = `balance` + '$amount1' WHERE `username` = '$uid'"); 
mysql_query("INSERT INTO `bets`(`amount`, `time_ago`, `username`, `multiplier`, `roll`, `result`) VALUES ('$amount2', '$time_ago', '$uid', '{$_POST['pay']}', '$rand', '$result')"); 

} 

else if($rand > $_POST['chance']) { 
    echo '<h3>You rolled a <strong>' .$rand. '</strong> out of 100 on the percentile dice! You lost...</h3>'; 
    $result = 'Loss'; 
    mysql_query("UPDATE `users` SET `balance` = `balance` - '$amount2' WHERE `username` = '$uid'"); 
    mysql_query("INSERT INTO `bets`(`amount`, `time_ago`, `username`, `multiplier`, `roll`, `result`) VALUES ('$amount2', '$time_ago', '$uid', '{$_POST['pay']}', '$rand', '$result')"); 

} 

} 
else { echo '<h3>You can only bet an amount you\'re capable of paying for!</h3>'; } 
} 
else if($amount2 > 0) { echo '<h3>You need to have a <strong>balance</strong> greater than 0 to bet. Sorry!</h3>'; } 
} 

} 

?> 

자바 스크립트 코드 :

$("#sub").click(function() { 
$.post($("#myForm").attr("action"), 
    $("#myForm :input").serializeArray(), 
    function(info){ $("#result").html(info); 
}); 
clearInput(); 
}); 

$("#myForm").submit(function() { 
return false;  
}); 

function clearInput() { 
$("#myForm :input").each(function() { 
    $(this).val(''); 
}); 
} 

답변

1

당신은 정말, 제대로 코드 들여 쓰기를 할 필요가, 그렇지 않으면 거의 읽을 수 있습니다.

은 당신의 자바 스크립트를 교체 :

$(function() { 
    $("#myForm").on('submit', function(e) { 
     e.preventDefault(); 
     $.ajax({ 
      type: 'POST', 
      url : $(this).attr('action'), 
      data: $(this).serialize() 
     }).done(function(info) { 
      $('#result').html(info); 
     }).fail(function(a,b,c) { 
      console.log('an error occured'); 
      console.log(a); 
      console.log(b); 
      console.log(c); 
     }); 
     $(":input", this).not('#sub').val(''); 
    }); 
}); 

것은 다음은 적절한 이벤트가 될 것 형태의 이벤트를 제출하고,이 클릭 버튼을 제출할 때 트리거 것, 당신은 버튼에 대한 이벤트 처리기가 필요하지 않습니다 .

PHP와 데이터베이스를 테스트하기가 어렵지만 fail() 메서드를 추가했지만 적어도 실패하면 오류 메시지가 나타납니다.

편집 :

이 정확하게 질문에, 당신은 전체 HEAD 섹션을 누락과 같은 마크 업, 그리고 스크립트가 BODY 섹션에로드 된 경우.

적어도 HEAD 태그와 제목을 추가해야합니다.

+0

죄송합니다. 나는 그것을 stackoverflow에 표시하기 위해 모든 서식을 다 망쳤습니다. 전체 스크립트를 해당 코드로 대체해야합니까? –

+0

예, 질문의 모든 부분이 위의 JS – adeneo

+0

으로 대체 될 수 있습니다. @ChuckBartowski - 실제로 몇 가지 변화를 시도해보십시오! – adeneo