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

时间:2013-08-09 16:27:55

标签: php javascript html

您好我在提交表单时遇到一些问题而没有刷新,我知道它与“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 

?>

2 个答案:

答案 0 :(得分:1)

除非您使用的是AJAX,否则您无法通过PHP实现这一目标。一旦表单提交,就是这样。可以在浏览器中进行简单验证。将验证功能绑定到表单的提交事件。这就是你从中返回虚假或真实的东西。

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

答案 1 :(得分:0)

它从您的代码看起来就像您正在尝试运行一些PHP代码(tradeformresult.php)。以这种方式加载它不会按预期工作 - require_once将在PHP中构建,而不是在浏览器中构建。

要在不刷新页面的情况下发送表单,您应该查看AJAX(http://en.wikipedia.org/wiki/Ajax_(programming)

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>标签,因为你将手动构建查询字符串。您可以使用从按钮的onClick事件触发AJAX调用的函数。