简体   繁体   English

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

[英]How to process submission without refreshing the page

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. 嗨,我在提交表格时没有刷新就遇到了一些问题,我知道它与“ return false”有关,但是我只是不知道在哪里以及如何使用它。 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 

?>

Unless you are using AJAX, you can't really do this from PHP. 除非您使用的是AJAX,否则您无法从PHP真正做到这一点。 Once a form submits, that's it. 表单提交后就可以了。 Simple validation can be done in the browser. 可以在浏览器中完成简单的验证。 Bind a validation function to your form's submit event. 将验证功能绑定到表单的Submit事件。 That's the thing you return false or true from. 那就是你从中返回false或true的东西。

(You would of course validate again on the server.) (您当然会在服务器上再次进行验证。)

It looks from your code like you're trying to run some php code (tradeformresult.php). 从您的代码看来,您似乎正在尝试运行一些php代码(tradeformresult.php)。 Loading it this way isn't going to work as expected-that require_once will be run as the page is being built in PHP, not in the browser. 用这种方式加载它并不能按预期的方式工作-require_once将在用PHP(而非浏览器)构建页面时运行。

For sending a form without refreshing the page, you should look into AJAX ( http://en.wikipedia.org/wiki/Ajax_(programming) ) 要发送表单而不刷新页面,您应该查看AJAX( http://en.wikipedia.org/wiki/Ajax_ ( programming)

JQuery has a good AJAX method. jQuery具有良好的AJAX方法。 Here is a simple example of how to use it: 这是一个简单的用法示例:

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

The above will call the given URL with the given data as parameters, then, if successful, will return whatever data the server gave back into the returnData variable. 上面的代码将使用给定的URL作为参数调用给定的URL,然后,如果成功,则将服务器提供的所有数据返回给returnData变量。

If you're using AJAX, you don't really even have to use a <form> tag, since you'll be building the query string manually. 如果您使用的是AJAX,则实际上甚至不必使用<form>标记,因为您将手动构建查询字符串。 You can have the function that makes the AJAX call be triggered from the onClick event of a button. 您可以具有使AJAX调用从按钮的onClick事件触发的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM