2017-02-22 77 views
0

我想,當用戶在提交表單點擊它來改變我的「提交」按鈕樣式。PHP文件將不會顯示錶單數據

action屬性調用PHP文件,但實際的數據不顯示,直到我從我的表單中刪除onSubmit屬性。然後我不能使用sendBtn()函數來改變按鈕的樣式。

僞類選項也不好,因爲它會改變風格,即使形式是空的。

任何想法?

PS。我在本地服務器上使用MAMP並使用Dreamweaver進行編輯。

<form class="customform" method="post" action="emailOnServer.php" method="post" onSubmit="return sendBtn();" autocomplete="on" > 
 
    <div class="s-12" onClick="formFocus()"><input id="imie" name="imie1"    placeholder="Twoje imię" type="text" required </input></div> 
 
    <div class="s-12" onClick="formFocus()"><input id="email" name="email1" placeholder="Twój e-mail" type="email" required </input></div> 
 
    <div class="s-12" onClick="formFocus()"><textarea placeholder="Wiadomość" id="pole" rows="5" style="resize:none;" required></textarea </input></div> 
 
    <div class="custom2" style="width: 34%; float: right;" ><input id="se" type="submit" value="Wyślij" style="background-color:#92c500; color: white; border-color:#6C0;" ></input> </div>   
 
    <div class="custom1" style="width: 65%;" ><button id="resetButton" onclick="cleanBtn()" type="reset" style="background-color: #808080; color: white; border-color:#066; border-radius:2px;">Wyczyść </button></div> 
 
    <img id="tick123" src="img2/green-tick-icon-0.png" style="display: none; float:right; position:absolute; right:1%; top:86%; padding:0; margin:0; width: 28px; height: 28px;"/> 
 
    <img id="tick1234" src="img2/green-tick-icon-0.png" style="display: none; position:absolute; left:58%; top:86%; padding:0; margin:0; width: 28px; height: 28px;"/> 
 
</form> 
 

 
<!-- more code to change form style on focus --> 
 

 
function sendBtn() { 
 
\t \t 
 
\t \t 
 
\t \t if (document.getElementById('email').value.indexOf("@")> 0 && 
 
\t \t  document.getElementById('email').value.indexOf(".")> 2 && 
 
\t \t  document.getElementById('imie').value != 0 && 
 
\t \t  document.getElementById('email').value != 0 && 
 
\t \t \t document.getElementById('pole').value != 0){ 
 
\t \t \t 
 
\t \t document.getElementById('se').style.backgroundColor = "#00283A"; 
 
\t \t document.getElementById("tick123").style.display="block"; 
 
\t \t document.getElementById('se').value="Wysłano"; 
 
\t \t document.getElementById('email').disabled=true; 
 
\t \t document.getElementById('imie').disabled=true; 
 
\t \t document.getElementById('pole').disabled=true; 
 
\t \t return true;}}

這是從一個免費的模板,我知道我應該使用CSS樣式表,以使其更具可讀性

這是emailOnServer.php文件:

<body> 
 
Hello User 
 
<?php $name = $_POST['imie1']; 
 
\t \t echo $name; ?> 
 

 
</body>

+0

請向我們展示sendBtn()函數。確保它返回正確的值來發送表單。 –

+0

我添加了更多的代碼,它位於''標記之間。 據我所知'真'將提交表格,它確實。然後,我在另一個選項卡中獲得.php文件,但它僅顯示「Hello User」。 – besthost

+0

刪除標題標題 –

回答

0

首先,你的HTML有一些錯誤,例如:

<input id="imie" .... type="text" required </input> 
// look here        ^
// the tag is not closed correctly 

應該

<input id="imie" name="imie1" placeholder="Twoje imię" type="text" required /> 

無論如何,如果你需要刪除onsubmit屬性,你可以做這樣的事情

<form name="myform" ...> 
    ... 
</form> 

屆時,JS

var form = document.forms.myform; 

form.onsubmit = function(){ 
    // do stuff 
    document.getElementById('se').style.backgroundColor = "#00283A"; 
    // ... 
} 

這個jsfiddle有一個適合您需求的示例代碼。表單元素被簡化了。

+0

OMG!謝謝:)羅赫略·我 簡單地這樣做: '變種形式= document.forms.myForm; form.onsubmit = sendBtn();' 和它的工作! 它與''標記沒有任何關係,因爲我在 – besthost

+0

之前嘗試了兩種方法你是對的!這是另一種方式。 – rogelio