Maybe this is a strange question. I have many button pass value with jquery. they are very similar. so i tried to combine them with number increase.
index.php
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
for (var i = 0; i <5; i++){
$('.click'+i).click(function(){
$.ajax({
url: "data.php",
dataType: "html",
type: 'POST',
data: "data"+i+"="+i,
success: function(data){
$("#result"+i).html(data);
}
});
});
}
});
</script>
<a class="click1">aaa</a>
<a class="click2">bbb</a>
<a class="click3">ccc</a>
<a class="click4">ddd</a>
<a class="click5">eee</a>
<?php
for ($i=1; $i<=5; $i++){
echo '<div id="result'.$i.'"></div>';
}
?>
but $_POST['data'.$i] is an unexpected T_ECHO, so how to write so that I will not reapeat work in data.php
data.php
<?php
for ($i=1; $i<=5; $i++){
$_POST['data'.$i]{
echo $_POST['data'.$i];
//some stuff, most are similer, less code diferent use if else for $_POST['data1'] - $_POST['data5']
}
}
?>
is ok
Also it’s possible to use
data[]ordata[5]as field name.It will be available in array
$_POST['data']