2012-02-17 29 views
5

Örneğin, her defasında bir düğme üreten bir döngünüz olduğunu varsayalım. Yani döngü beş kez koştuysa, 5 düğme olacaktır. Şimdi, döngü her döngüsünde değişecek olan $ num gibi bir değişkenim var. Düğmeye tıklandığında her düğmenin bireysel $ değerini geçecek şekilde bir yolu var mı?Bir php değişkenini bir düğme (html/php) kullanarak başka bir sayfaya iletmenin bir yolu var mı?

$num = 0; 
while(...){ 
    //want this to send $num via POST or GET to xyz.php 
    echo '<button type="submit" value="3" name="editId">Remove</button>'; 
    $num++;//$num changes every instance of the loop 
} 

cevap

1

düğmenin adı & değeri formu ile gönderilmelidir.

Örneğin

: düğmesi "Düğme" tıklandığında

<?php print_r($_POST); ?> 

<form action="" method="post"> 
    <button type="submit" name="button" value="value">Button</button> 
</form> 

form sunulacak ve

Array ([button] => value) 

formu üzerinde çıktı olacaktır.

$num = 0; while(...){  //want this to send $num via POST or GET to xyz.php 
echo '<button type="submit" value="'.$num.'" name="editId">Remove</button>';  
$num++;//$num changes every instance of the loop 
} 
0

:

durumunuza bu uygulama, tek gibi bir şey kullanabilirsiniz :

$num = 0; 
while(...){ 
    //want this to send $num via POST or GET to xyz.php 
    echo '<form method="post" action="xyz.php">'; 
    // Pass $num into the form value 
    echo '<button type="submit" value="' . $num. '" name="editId">Remove</button>'; 
    echo '</form>' 
    $num++; 
} 

Hangi tıklatılmışsa geçirilir xyz.php'a $_POST['editId'] olarak.

Bu yöntem her düğme için farklı bir form oluştururken, tek bir form kullanacak ve değeri tıklatılan düğmeyi temel alarak bir gizli girdiye atayacak olan JavaScript'i kullanmanın başka yolları da vardır. Gönderilen yalnızca bir giriş değeri ise, birden çok <form> s seçeneğinin kodlanması en kolay olanıdır.

+0

@High life of life, şimdi bu komik. Bir an için cevabımı kopyaladığınızı/yapıştırdığınızı düşündüm. :) Sanırım ilk önce cevapladın. Ve sanırım ikimiz de OP'in bunları bir

TecBrat

+0

LOL !! Bu çok komik ... Bir an için cevap verdiğim bir anda iki kere yayınladım, çünkü ikimiz de aynı biçimi birleştirmek için kullandık. –

4

kolay yolu <button> etiketleri her çevreleyen <form> oluşturmaktır: Neredeyse geldin

$num = 0; 
while(...){ 
    // Set $num as the value of each button 
    echo "<button type='submit' value='{$num}' name='editId'>Remove</button>"; 
    $num++;//$num changes every instance of the loop 
} 
+0

Bunu yapmayla ilgili sorun, gereksiz olan 5 (veya daha fazla) '' etiketine sahip olmanızdır. –

+0

Teşekkürler, aradığım şey bu! –

+0

Birden çok form gereksizdir. Birden fazla '' '' '' '' '' '' '' 'forms forms forms forms forms forms forms forms forms forms forms forms forms forms form form form form form form form form form form form form form form form form form form form form form to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to to w w w w w w w to to to to to to to to w –

0

Çok yakınsınız. Sadece $num düğmenizin value özniteliğine ekleyin. $_POST['editId']:

$num = 0; 
while(...){ 
    //want this to send $num via POST or GET to xyz.php 
    echo '<button type="submit" value="'.$num.'" name="editId">Remove</button>'; 
    $num++;//$num changes every instance of the loop 
} 

size $num değerini gönderir [ Remove ] butonuna tıklayarak, <form> etiketleri bu kod bloğunu çevreleyen

.

İlgili konular