2014-04-06 122 views
0

我想發佈一個表單到$ wp_session這是工作正常 - 但是當我再次執行它覆蓋我放在那裏的數組,首先,我需要數組像收集/購物籃一樣進行建設。

我的形式:

<form class="form1" method="post" action="" id="form1"> 
<fieldset> 
<ul> 
     <label for="name">Exercise ID</label><span> 
     <input type="text" name="ExerciseID" placeholder="Exercise ID" class="required" role="input" aria-required="true"/></span> 

     <label for="name">Exercise Reps</label><span> 
     <input type="text" name="Sets" placeholder="Sets" class="required" role="input" aria-required="true"/></span> 

     <label for="name">Exercise Reps</label><span> 
     <input type="text" name="Reps" placeholder="Sets" class="required" role="input" aria-required="true"/></span> 

     <input class="submit .transparentButton" value="Next" type="submit" name="Submit"/> 

</ul> 
<br/> 
</fieldset> 
</form> 

而且PHP:

<?php 

global $wp_session; 

if (isset($_POST['Submit'])) { 
$wp_session['collection'] = array($POST['ExerciseID'],$_POST['Sets'],$POST['Reps']); 

} 

print_r($wp_session); 

?> 

提前非常感謝。

+0

首先,數組中缺少一些下劃線($ POST ['ExerciseID'],$ _ POST ['Sets'],$ POST ['Reps'])'try'array($ _ POST ['ExerciseID' ],$ _ POST ['Sets'],$ _ POST ['Reps'])'這些是超全局變量,其中99%需要變爲'$ _XXX' –

回答

1

爲了有一個籃子,你必須將產品添加到一個數組:

<?php 

global $wp_session; 

if (isset($_POST['Submit'])) { 
$wp_session['collection'][] = array($_POST['ExerciseID'],$_POST['Sets'],$_POST['Reps']); 

} 

print_r($wp_session); 

?> 

您可能希望通過ExerciseID組織陣列,這樣你就可以添加量。我會建議你創建一些對象,他們會比陣列

+0

您遺漏了OP錯過了兩個下劃線的部分。根據['我的評論'](http://stackoverflow.com/questions/22895443/wp-session-variables-being-overwritten-by-post#comment34939699_22895443);-) –

+0

OP問題是變量被覆蓋問題是如何將它添加爲購物環境,而不是數據不正確(因爲錯誤的值將作爲空值) –

+0

我知道,我只是說「其中一個」問題是兩個缺失的下劃線。通常我們指出這些錯誤。 –

1

也許你正在尋找這種更靈活:而不是覆蓋你追加一個新的條目到你的「會話變量」在每次運行:

<?php 

global $wp_session; 

if (isset($_POST['Submit'])) { 
$wp_session[]['collection'] = array($_POST['ExerciseID'],$_POST['Sets'],$_POST['Reps']); 
} 

print_r($wp_session); 
?> 

請注意在作業左側額外的[]

+0

根據['Read this'](http://stackoverflow.com/questions/22895443/wp-session-variables-being-overwritten-by -post#comment34939699_2289544 3)你也犯了同樣的錯誤。 –

+0

不錯,謝謝提及。修正了。儘管實際上這不是OP的問題。 – arkascha

+0

不客氣。你可能想在你的回答中加上,說明兩個缺少的下劃線。我知道這一點,但指出錯誤是很好的。 –