I have a jquery function which sends data to php. I get a value of text element by jquery. When I send data to php this data is multiplied. In first request jquery sends normal data, in second doubled data, in third tripled...
I tried a lot of methods but in all result is the same. Below I added source code.
<p id="admAddH">Enter an email address of a new administrator:</p>
<p><input type="text" id="addAdmIR" /></p>
<p><button id="addAdmBtn">Add</button></p>
<script>
$.get('create_page.php?a=adminsAdd2&id='+id+'&admin='+v1,{addAdmIR:$(this).val()},function(data){
$('#noRefreshAdmAD').empty();
$('#noRefreshAdmAD').html(data);
$('#addAdmIR').val('');
});
</script>
And php:
$id = $_GET['id'];
$admin = $_GET['admin'];
mysql_query("INSERT INTO `pages_admin` (`admin`,`pId`,`nameType`,`privacy`,`admins`,`ownshp`,`delPg`) VALUES ('$admin','$id','1','1','0','0','0')");
$query3 = mysql_query("SELECT * FROM `pages` WHERE `id`='$id' AND (`email`='".$_SESSION['user']."')");
$query9 = mysql_query("SELECT * FROM `pages_admin` WHERE `pId`='$id'");
$adQua = mysql_num_rows($query9);
while($row=mysql_fetch_array($query3)) {
echo'
<div id="adQuaAdmD">The Business Page “'.$row['name'].'” has '.$adQua.' administrators.</div>
<div id="adAdmList">';
while($row = mysql_fetch_array($query9)) {
echo'
<div id="adAdmListItem">• '.$row['admin'].'
<span id="adRemoveAdm">
<span delete-admin="'.$row['id'].'">[x]</span>
</span>
</div>
';
}
echo'</div>';
Thanks for help
One note, I don't see where you get your id
from, so I left that out. Try using something like this:
index.php
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<!-- wrap with form tags -->
<form id="add_admin">
<p id="admAddH">Enter an email address of a new administrator:</p>
<p><input type="text" name="email" /></p>
<p><input type="submit" value="Add" /></p>
</form>
<div id="resp"></div>
<script>
$("#add_admin").submit(function() {
$.ajax({
url: 'create_page.php',
type: 'post',
data: { add: "admins", email: $("input[name='email']").val() },
success: function(response) {
$("#resp").html(response);
}
});
// Return false so the page doesn't reload
return false;
});
</script>
create_page.php
<?php
session_start();
// You should do some sort of input validating here
$id = (!empty($_POST['id']) && is_numeric($_POST['id']))? $_POST['id'] : mt_rand();
$admin = (filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))? $_POST['email']:false;
// Don't do anything if the admin fails validation
if(!$admin)
return;
// On important note, you should not be using mysql_ anymore. Use
// PDO or mysqli_ with prepared/bound statements/values
mysql_query("INSERT INTO `pages_admin` (`admin`,`pId`,`nameType`,`privacy`,`admins`,`ownshp`,`delPg`) VALUES ('$admin','$id','1','1','0','0','0')");
$query3 = mysql_query("SELECT * FROM `pages` WHERE `id`='$id' AND (`email`='".$_SESSION['user']."')");
$query9 = mysql_query("SELECT * FROM `pages_admin` WHERE `pId`='$id'");
$adQua = mysql_num_rows($query9);
$i = 1;
while($row = mysql_fetch_array($query3)) { ?>
<!-- You should have unique id names, so adding a number
is probably the easiest way to do that -->
<div id="adQuaAdmD<?php echo $i; ?>">The Business Page “<?php echo $row['name'].'” has '.$adQua; ?> administrators.</div>
<div id="adAdmList<?php echo $i; ?>">
<?php
$a = 0;
while($row = mysql_fetch_array($query9)) { ?>
<div id="adAdmListItem<?php echo $a; ?>">• <?php echo $row['admin']; ?>
<span id="adRemoveAdm<?php echo $a; ?>">
<span delete-admin="<?php echo $row['id']; ?>">[x]</span>
</span>
</div>
<?php
$a++;
} ?>
</div>
<?php $i++;
} ?>