2013-09-23 70 views
0

大家好,我是一個新的Web開發學習者。如何使按鈕在同一頁面上執行,但不在下一頁

用戶點擊提交按鈕後,我有一個頁面,然後按鈕將表單內容提交到下一頁執行並保存到數據庫。

因此,我的頁面將轉到另一頁執行並返回到目標頁面。

如:index.php文件和exec.php

的index.php:

<form name="g-form" action="gbtn-exec.php" method="post" class="goat-vote" onsubmit="return validategForm()"> 
<input type="text" name="g-product" placeholder="Brand/Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" /> 


<p class="g-question">Why you love it?</p> 

<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea> 

<input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;"></form> 

exec.php

if ($_POST["g-product"] && $_POST["g-reason"] != "") 
{ 
$gproduct = $_POST["g-product"]; 
$greason = $_POST["g-reason"]; 

$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')"; 
$result = mysql_query($insert,$con); 
echo "<script>"; 
echo "alert('Thank you. Your vote has been recorded.');"; 
echo "window.location.href='index.php';"; 
echo "</script>"; 
} 

我的問題是,我怎麼能執行的提交按鈕沒有去exec.php的index.php? (意味着將兩者結合在一個.php中) 這是因爲當用戶點擊提交按鈕時,它會進入空白頁面執行,看起來不太好。

任何人都可以提供幫助嗎?謝謝! =)

+2

使用ajax方法在後臺執行exec.php並在index.php中檢索結果 –

+0

嘗試使用jquery ajax – iJade

+0

@DonovanCharpin我可以知道該如何編寫腳本?我有點不明白maunal ... =( – youaremysunshine

回答

0
if (isset($_POST["g-btn"])) 
{ 
$gproduct = $_POST["g-product"]; 
$greason = $_POST["g-reason"]; 

$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')"; 
$result = mysql_query($insert,$con); 
echo "<script>"; 
echo "alert('Thank you. Your vote has been recorded.');"; 
echo "window.location.href='index.php';"; 
echo "</script>"; 
} 

<html> 
<form name="g-form" action="a.php" method="post" class="goat-vote" onsubmit="return validategForm()"> 
<input type="text" name="g-product" placeholder="Brand/Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" /> 


<p class="g-question">Why you love it?</p> 

<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea> 

<input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;"></form> 
</html> 

你可以這樣做... isset($ _ POST [「g-btn」])會檢查它是否被點擊或不是?

0

編輯:忽略此請,沒有看過你的問題完全,因爲它似乎

嘗試是這樣的:

的index.php

<form name="g-form" action="gbtn-exec.php" method="post" class="goat-vote" onsubmit="return validategForm()"> 
    <input type="text" name="g-product" placeholder="Brand/Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" /> 

    <p class="g-question">Why you love it?</p> 

    <textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea> 

    <?php if(!isset($_GET['hidesubmit'])): ?> 
    <input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;"> 
    <?php endif; ?> 
</form> 

高管。 php

if ($_POST["g-product"] && $_POST["g-reason"] != "") 
{ 
    $gproduct = $_POST["g-product"]; 
    $greason = $_POST["g-reason"]; 

    $insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')"; 
    $result = mysql_query($insert,$con); 
    echo "<script>"; 
    echo "alert('Thank you. Your vote has been recorded.');"; 
    echo "window.location.href='index.php?hidesubmit=1';"; 
    echo "</script>"; 
} 
+0

對不起,但爲什麼它不工作? – youaremysunshine

+0

它應該工作,但你想要一個解決方案,無需發送表單 - 我的解決方案發送表單並重定向到原始頁面特殊參數,在此參數的基礎上顯示提交按鈕。 – Joshua

0

添加的jQuery的lib在你的頭和你的jQuery perso(myjQuery.js),您將使用後的ajax

的index.php

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
<script src="/js/myjQuery.js"></script> 

沒有你的表格上的操作創建表單。

<html> 
    <form name="g-form" class="goat-vote"> 
     <input type="text" name="g-product" placeholder="Brand/Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" /> 
     <p class="g-question">Why you love it?</p> 
     <textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea> 
     <input name="g-btn" class="vote-btn" value="vote" style="margin-left:470px; cursor:pointer;"> 
    </form> 
</html> 

讓您exec.php這樣

exec.php

if ($_POST["g-product"] && $_POST["g-reason"] != "") 
{ 
    $gproduct = $_POST["g-product"]; 
    $greason = $_POST["g-reason"]; 
    $insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')"; 
    $result = mysql_query($insert,$con); 
    echo "<script>"; 
    echo "alert('Thank you. Your vote has been recorded.');"; 
    echo "window.location.href='index.php?hidesubmit=1';"; 
    echo "</script>"; 
} 

,並創建myjQuery.js

myjQuery.js

$(document).ready(function(){ 
    $(".vote-btn").on("click", function(){ 
     g-product = $("input[name='g-product']").val(); 
     g-reason = $("input[name='g-reason']").val(); 
     $.ajax({cache:false,dataType:'html',crossDomain:true,type:'POST', 
      url:'/php/exec.php',data : {g-product:g-product, g-reason:g-reason}, 
      success:function(html){ 
       console.log(html); 
      }, 
      error:function(j,t,e){ 
       console.log('problem'); 
      } 
     }); 
    }); 
}); 

你可以試試這個代碼,也許你的文件的js路徑或php路徑存在問題。也許可以糾正一些錯誤。

相關問題