0

Hi I am facing some problems in submitting form without refreshing, I know that it has something to do with "return false " but i just dont know where and how to use it. I tried refreshing the page by placing it in (if there are errors) but it just doesnt seem to work. Can you guys help me out??

<?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 

?>
4

2 回答 2

1

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

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

于 2013-08-09T16:30:52.287 回答
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 调用的功能。

于 2013-08-09T16:33:57.603 回答