2012-11-27 27 views
0

我有作为数组传递到URL和页面重新加载的复选框我应该让它们回来以便检查所检查的内容。

这里是我的javascript,阵列中的问题是 'entry_reason':

<script language="JavaScript"> 
<!-- 
function reload(nname, surname, birth_date, birth_country, birth_place, postal_code, address, residence, oib, license_number, license_iplace, passport_number, passport_iplace, entry_reason, myself, myselfeng) 

{ 
// Setting the variable with the value of selected country's ID 
var nname=document.getElementById('nname').value; 
var surname=document.getElementById('surname').value; 
var birth_date=document.getElementById('birth_date').value; 
var c=document.getElementById('birth_country').value; 
var city=document.getElementById('birth_place').value; 
var postal_code=document.getElementById('postal_code').value; 
var address=document.getElementById('address').value; 
var residence=document.getElementById('residence').value; 
var oib=document.getElementById('oib').value; 
var license_number=document.getElementById('license_number').value; 
var license_iplace=document.getElementById('license_iplace').value; 
var passport_number=document.getElementById('passport_number').value; 
var passport_iplace=document.getElementById('passport_iplace').value; 
var entry_reason=[document.getElementById('entry_reason1').value, document.getElementById('entry_reason2').value, document.getElementById('entry_reason3').value, document.getElementById('entry_reason4').value, document.getElementById('entry_reason5').value, document.getElementById('entry_reason6').value, document.getElementById('entry_reason7').value]; 
var myself=document.getElementById('myself').value; 
var myselfeng=document.getElementById('myselfeng').value; 
// Sending the country id in the query string to retrieve the city list 
self.location='blacklistprivateregister.php?nname=' + nname + '&surname=' + surname + '&birth_date=' + birth_date + '&birth_country=' + c + '&birth_place=' + city + '&postal_code=' + postal_code + '&address=' + address + '&residence=' + residence + '&oib=' + oib + '&license_number=' + license_number + '&license_iplace=' + license_iplace + '&passport_number=' + passport_number + '&passport_iplace=' + passport_iplace + '&entry_reason[]=' + entry_reason + '&myself=' + myself + '&myselfeng=' + myselfeng; 
} 
--> 
</script> 

我得到URL看起来是这样的:

http://xxxxxxxx.php?nname=ivo&surname=&birth_date=2012-11-08&birth_country=Algeria&birth_place=Benguela&postal_code=10000&address=%C4%8Ci%C4%8Dkovina%20222&residence=Zagorne&oib=1231231232131&license_number=21312323213&license_iplace=Zagreb&passport_number=000493243244&passport_iplace=Vara%C5%BEdin&entry_reason[]=1,2,3,4,5,6,7&myself=aasddsad&myselfeng=asdadasd 

将与我的输入复选框是这样的:

<legend><?php echo REASONFORENTRY_R; ?></legend> 
     <?php 
      $e=array(); 
      if(isset($_POST['entry_reason'])){ $e=$_POST['entry_reason'];} 
      if($_GET['entry_reason']) {$e=$_GET['entry_reason'];} 
     ?> 
     <p><input type="checkbox" name="entry_reason[]" id="entry_reason1" class="check" value="1" <?php if(in_array('1', $e)) echo 'checked="checked" '; ?>/><?php echo ER1_R; ?></p> 
     <p><input type="checkbox" name="entry_reason[]" id="entry_reason2" class="check" value="2" <?php if(in_array('2', $e)) echo 'checked="checked" '; ?>/><?php echo ER2_R; ?></p> 
     <p><input type="checkbox" name="entry_reason[]" id="entry_reason3" class="check" value="3" <?php if(in_array('3', $e)) echo 'checked="checked" '; ?>/><?php echo ER3_R; ?></p> 
     <p><input type="checkbox" name="entry_reason[]" id="entry_reason4" class="check" value="4" <?php if(in_array('4', $e)) echo 'checked="checked" '; ?>/><?php echo ER4_R; ?></p> 
     <p><input type="checkbox" name="entry_reason[]" id="entry_reason5" class="check" value="5" <?php if(in_array('5', $e)) echo 'checked="checked" '; ?>/><?php echo ER5_R; ?></p> 
     <p><input type="checkbox" name="entry_reason[]" id="entry_reason6" class="check" value="6" <?php if(in_array('6', $e)) echo 'checked="checked" '; ?>/><?php echo ER6_R; ?></p> 
     <p><input type="checkbox" name="entry_reason[]" id="entry_reason7" class="check" value="7" <?php if(in_array('7', $e)) echo 'checked="checked" '; ?>/><?php echo ER7_R; ?></p> 

我在网站上有3个动作,其中2个我想让复选框被记住: 1)页上的javascript函数重载 2)页面重新加载错误提交重装 - 这一个适用于现在$ _ POST

我相信,对于第一次重装我应该从URL所有变量项目到$ _GET和通他们到$ e变量,但它似乎并没有工作。

所有帮助表示赞赏!

+0

没有违法,但你有没有听说过循环?你知道'

'html元素,它用于什么?关于'GET'和'POST' http请求方法的相同问题。您是否考虑过对所有请求使用单个http方法(即GET **或** POST)?作为最后的解决方法,你是否试图使用'$ _REQUEST'超级全局而不是'$ _GET'和'$ _POST'? – J0HN

回答

-1

在PHP中使用json_encode($array);

http://php.net/json_encode

在Javascript中你有很多方法来皮肤猫。我的解决方案是将此Array序列化并将其发送回PHP。

http://api.jquery.com/serializeArray/

+0

在您的表单中,您可以使用jQuery'vars = $('#form')。serialize()'并将它发送给PHP –