如何在不刷新页面的情况下处理提交


How to process submission without refreshing the page

嗨,我在不刷新的情况下提交表单时遇到了一些问题,我知道这与"return false"有关,但我不知道在哪里以及如何使用它。我试着通过放置它来刷新页面(如果有错误),但它似乎不起作用。你们能帮我吗??

<?php 
    $message = '';
    $errors = array();
    $noErrors = true;
    $haveErrors = !$noErrors;
    require_once('validations/tradeformresult.php');
        if ($noErrors && $userArriveBySubmittingAForm) {
        require_once('price.php');// INSERTION
        echo "<script type='text/javascript'>'n"; 
            echo "</script>";
        echo "<script type='text/javascript'>'n"; 
        echo "alert('Trade is successfully executed!');'n"; 
        echo "</script>"; 

    ///////////MESSAGE///////////////// 
    }
         elseif ($haveErrors && $userArriveBySubmittingAForm) {
        echo "<script type='text/javascript'>'n"; 
        echo "alert('Please re-enter your parameters.');'n"; 
echo "return false"; 
            echo "</script>";

     }

     else if ($userArriveByClickingOrDirectlyTypeURL) { // we put the original form inside the $message variable
        $newTitle = 'The link is broken';
        $h1Title = '';
        $message = '';
    }
?>
<!DOCTYPE html>
<html>
<head>      
<script type="text/javascript">
</script><head><meta charset="UTF-8"></head>
<style type="text/css">
div#overlay {
    display: none;
    z-index: 2;
    background: #000;
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    text-align: center;
}
div#specialBox {
    display: none;
    position: relative;
    z-index: 3;
    p.padding;
    padding-top:25px;
    padding-bottom:25px;
    padding-right:50px;
    padding-left:50px;
    margin: 150px auto 0px auto;
  border: 3px solid blue;
  outline: 3px solid darkblue;
    width: 500px; 
    height: 500px;
    overflow:auto;
    background: #FFF;
    color: #000;
}
div#wrapper {
    position:absolute;
    top: 0px;
    left: 0px;
    padding-left:24px;
}
</style>
<script type="text/javascript">

function toggleOverlay(){
    var overlay = document.getElementById('overlay');
    var specialBox = document.getElementById('specialBox');
    overlay.style.opacity = .8;
    if(overlay.style.display == "block"){
        overlay.style.display = "none";
        specialBox.style.display = "none";
    } else {
        overlay.style.display = "block";
        specialBox.style.display = "block";
    }
}
</script>
</head>
<body>
<!-- Start Overlay -->
<div id="overlay"></div>
<!-- End Overlay -->
<!-- Start Special Centered Box -->
<div id="specialBox" style="display:none">
<script>
</script>
<p>Create Order
  <p><?php
$timestamp=time(); require_once 'start.php';
?>

<form method="post" name="formSubmitted" **return false;"**>
<input type="hidden" name="formSubmitted" value="true" runat="server">
<?php echo $message; ?>
<?php ?>
<?php if ($haveErrors || $userArriveByClickingOrDirectlyTypeURL) : ?>
    <fieldset>
<p>Symbol : <select name = "selection" id="selection">
<option disabled = "disabled" selected = "selected"> Choose one </option>
<option value="eur/usd"<?php If($selection=='eur/usd'){Echo 'selected';}?>>EUR/USD</option>
<option value="usd/jpy"<?php If($selection=='usd/jpy'){Echo 'selected';}?>>USD/JPY</option>
<option value="usd/cad"<?php If($selection=='usd/cad'){Echo 'selected';}?>>USD/CAD</option>
<option value="eur/jpy"<?php If($selection=='eur/jpy'){Echo 'selected';}?>>EUR/JPY</option>
<option value="eur/chf"<?php If($selection=='eur/chf'){Echo 'selected';}?>>EUR/CHF</option>
<option value="gbp/usd"<?php If($selection=='gbp/usd'){Echo 'selected';}?>>GBP/USD</option>
<option value="aud/usd"<?php If($selection=='aud/usd'){Echo 'selected';}?>>AUD/USD</option>
<option value="usd/chf"<?php If($selection=='usd/chf'){Echo 'selected';}?>>USD/CHF</option>
</select><font color="red"><?php echo $selectionError?></font>
<p> Date : <input type="datetime" value="<?php echo date("Y-m-d ",$timestamp); ?>"READONLY name="date"/></p>
<p> Type : <input type="radio" name="type" value="buy"<?php if ($type == 'buy') echo 'checked'; ?>CHECKED> Buy <input type="radio" name="type" value="sell" <?php if ($type == 'sell') echo 'checked'; ?>>Sell<font color="red"><?php echo $typeError;?></font></p>
<p> Size : <input type="number"pattern="[0-9]+(['.|,][0-9]+)?" step="0.01"min="0"name="size"value="<?php echo $size;?>"/><font color="red"><?php echo $sizeError?></font></p>
<p> Bid Price (Sell) : <input id="bidprice" READONLY name="bidprice" type="text" value="<?php echo $bidprice;?>"/><font color="red"><?php echo $bidpriceError?></font></p>
<p> Offer Price (Buy) :<input id="offerprice" READONLY name="offerprice" type="text" value="<?php echo $offerprice;?>"/><font color="red"><?php echo $offerpriceError?></font> </p>
<p> Stop Loss : <input type="number"step="any"min="0" name="stoploss" value="<?php echo $stoploss;?>"/><font color="red"><?php echo $stoplossError?></font></p>
<p> Take Profit : <input type="number"step="any"min="0"name="takeprofit"value="<?php echo $takeprofit;?>"/><font color="red"><?php echo $takeprofitError?></font></p>
</fieldset>
<div align="center">
<input type="submit" value="Submit" Onsubmit =**"return false"**;/><button onmousedown="toggleOverlay()">Close </button>
</div>
<input type="reset" name="Reset" value="Reset" tabindex="50">
<?php endif; ?>
</form>

 </script> 

</body>
</html></p>
</div>
</div>
<!-- Start Special Centered Box -->
<!-- Start Normal Page Content -->
<div id="wrapper">
  <h2>Trade</h2>
  <button onmousedown="toggleOverlay();**return false;"**>Create Order</button>
</div>

<!-- End Normal Page Content -->
</body>
</html>
<?php 
?>

除非您使用AJAX,否则您无法从PHP真正做到这一点。表单提交后就可以了。简单的验证可以在浏览器中完成。将验证函数绑定到表单的提交事件。这就是你返回虚假或真实的东西。

(您当然会在服务器上再次验证。)

从你的代码中看,你似乎正在尝试运行一些php代码(tradeformresult.php)。以这种方式加载它不会像预期的那样工作,因为require_one将在php中运行,而不是在浏览器中。

对于在不刷新页面的情况下发送表单,您应该研究AJAX(http://en.wikipedia.org/wiki/Ajax_(编程)

JQuery有一个很好的AJAX方法。以下是如何使用它的一个简单示例:

$.ajax({url:"http://www.someserver.com/api/path",
data:{val1:"value",val2:"value"})
.success(function(returnData) {
    console.log(returnData);
});

上面将使用给定的数据作为参数调用给定的URL,然后,如果成功,将服务器返回的任何数据返回到returnData变量中。

如果您使用AJAX,您甚至不必使用<form>标记,因为您将手动构建查询字符串。您可以拥有一个函数,使AJAX调用从按钮的onClick事件触发。