PHP submit button while loop - javascript

Hi guys im having a bit of a problem on how to get the value of the table row everytime i click an add button. what im trying to achieve is that to pass the value of the row to another php file for sql query. at the moment i have a table using a while loop and displaying a list of names from the database with a "ADD" button per row. When ever i click the add button regardless of which row i always get the value of the last row. can someone please point of on which part of the code i made a mistake?
here is my code
<form action="addFriend.php" method="post"> <table class="table table-bordered">
<?php
$i = 1;
while($row = mysqli_fetch_array($records))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="addedUser" value="<?php echo $row["username"]; ?>" readonly></td>
<td>
<button type="submit" name="row_<?php echo $i; ?>" id="row_user" value="<?php echo $row["username"]; ?>" class="btn btn-info">ADD</button>
<?php $i++; ?>
</td>
</tr>
<?php
}
?>
</table>
</form>

Try Below code. As you want only $row["username"] and it is already stored in button value. Add form in while loop and give same name for button so that you can get username in addFriend.php
<table class="table table-bordered">
<?php
$i = 1;
while($row = mysqli_fetch_array($records))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="addedUser" value="<?php echo $row["username"]; ?>" readonly></td>
<td>
<form action="addFriend.php" method="post">
<button type="submit" name="row" id="row_user" value="<?php echo $row["username"]; ?>" class="btn btn-info">ADD</button>
</form>
<?php $i++; ?>
</td>
</tr>
<?php
}
?>
</table>
Now in addFriend.php you can get value by $_POST['row'];

Try Below script
<form action="addFriend.php" method="post"> <table class="table table-bordered">
<?php
$i = 1;
while($row = mysqli_fetch_array($records))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="**addedUser[]**" value="<?php echo $row["username"]; ?>" readonly></td>
<td>
<button type="submit" name="row_<?php echo $i; ?>" id="row_user" value="<?php echo $row["username"]; ?>" class="btn btn-info">ADD</button>
<?php $i++; ?>
</td>
</tr>
<?php
}
?>
</table>
</form>
In addFriend.php
print form data using print_r($_REQUEST);

Related

How to add Value in jQuery

I want to add value to td in jquery. I have this code but not working.
Return all values for single td. I need that input [name="text-194] contain further values not all.
<table id="mytable" style="width: 100%;" border="1">
<tbody>
<?php
$counter = 1;
foreach ($result_list as $key => $row) { ?>
<tr>
<td class="jakas"><p><?php echo $counter; ?></p></td>
<td class="numer" value="<?php echo $row->numer; ?>"><p><?php echo $row->numer; ?></p></td>
<td class="nazwa" value="<?php echo $row->nazwa; ?>"><p><?php echo $row->nazwa; ?></p></td>
<td class="input" value="<?php htmlspecialchars($row->nazwa); ?>">
<?php echo do_shortcode('[contact-form-7 id="73" title="plik"]'); ?></td>
<?php echo '<input type="text" name="idtest" value="' . htmlspecialchars($row->nazwa) . '">'; ?>
</tr>
<?php $counter++; ?>
<script>
(function ($) {
$(document).ready(function () {
var value = '<?=htmlspecialchars($row->nazwa)?>';
console.log(value);
$('input[name="text-194"]').val($('input[name="text-194"').val() + value);
});
})(jQuery);
</script>
<?php
}
?>

Submit with one button multiple forms generated by loop

Sorry for spaghetti code :( I can't figure out why is it not working. Button submits only last form, but i want to submit every generated form. What can i do? I tried AJAX but it gives the same result. I wanted to recursively add selections but i think it's the hardest way and there is another solution.
<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Product Name</th>
<th>Product Weight</th>
<th>Product Image</th>
<th>Status</th>
<th>Choose suplier</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $k=>$v)
{
$oid[] = $v['op_id'];
$pui[] = $v['pui'];
?>
<tr>
<td><?php echo substr($v['name'], 0,10); ?></td>
<td><?php echo substr($v['gram'], 0,10); ?></td>
<td><img style="width: 50%" src="<?php echo $site_url.'images/'.$v['image']; ?>" alt=""></td>
<td><?php echo substr($v['opia'], 0,10); ?></td>
<td>
<form method="POST" enctype="multipart/form-data" id="demo-form2" data-parsley-validate oidd="<?php echo $v['pui']; ?>" class="form-horizontal form-label-left sels<?php echo $v['op_id'];?>">
<div class="form-group">
<label class="">
</label>
<div class="" style="width: 100%">
<?php
$sups = $db->prepare('SELECT * FROM product p, suplier s, prod_suplier ps WHERE p.u_id=ps.p_id AND ps.suplier_id=s.id AND p.u_id=:u_id GROUP BY s.id');
$sups->execute(array('u_id'=>$v['pui']));
$count_sups = $sups->rowCount();
if ($count_sups==1) {
$get_sups = $sups->fetch(PDO::FETCH_ASSOC);
echo $get_sups['full_name'];
} else {
?>
<select style="" name="supliers" id="">
<?php
while ($get_sup=$sups->fetch(PDO::FETCH_ASSOC)) {
?>
<option value="<?php echo $v['sfn']; ?>"><?php echo $get_sup['full_name']; ?></option>
<?php }?>
</select>
<?php } ?>
</div>
</div>
</form>
</td>
<td>
<form style=" float: right;" method="POST" action="<?php echo $site_url.$state.'delete/' ?>">
<input type="hidden" name="delid" value="<?php echo $v['oid'] ?>">
<button type="button" onclick="dsomo(this);" name="dbtn" style="color: red; background-color: rgba(0,0,0,0); border:none;" href="">
<i class="fa fa-trash fa-2x" ></i>
</button>
</form>
</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div>
</div>
<button type="submit" id="acception" name="submit">submit</button>
<script async>
$('#acception').on('click', function () {
<?php
for ($i=0;$i<count($oid);$i++) {
?>
$("tr>td>.sels<?php echo $oid[$i];?>").submit();
<?php } ?>
})
</script>
Thanks to MojoAllmighty I found the solution. As described in the link below if I want to submit multiple forms by one button I have to assign equal classes to form and use ajax for async submitting forms. So maybe it can be marked as a duplicate of a link below.
Jquery - Submit all forms by one button

My HTML Form element closing itself before even input element started

i tried to edit this soo much times but unable to do so. Please help me.
There is php Code also But it is not showing down. Above this is the if statement for the result more than zero and the only problem i got is that gender checked is working only in one form and in other form the gender checked don't worked. The form element is closing itself after starting. Please Help me i shall be thankful to you.
My Code is here.
<table>
<tr>
<th>S.NO.</th>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Phone</th>
<th>Gender</th>
<th colspan="2">Action</th>
<th>Delete</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$srnumber++;
?>
<tr class="<?php echo $row['id']; ?> tablerow">
<td><?php echo $srnumber;?></td>
<td class="<?php echo $row['id'];?> td3"><?php echo $row['name'];?></td>
<td class="<?php echo $row['id'];?> td4"><?php echo $row['email'];?></td>
<td class="<?php echo $row['id'];?> td5"><?php echo $row['address'];?></td>
<td class="<?php echo $row['id'];?> td6"><?php echo $row['city'];?></td>
<td class="<?php echo $row['id'];?> td7"><?php echo $row['state'];?></td>
<td class="<?php echo $row['id'];?> td8"><?php echo $row['phone'];?></td>
<td class="<?php echo $row['id'];?> td9"><?php echo $row['gender'];?></td>
<td><a onclick="userupdateclick(<?php echo $row['id']; ?>);" class="update" data-value="<?php echo $row['id']; ?>">Update</a></td>
<td></td>
<td><a onclick="deleteuser(<?php echo $row['id']; ?>);" href="#" class="delete" data-value="<?php echo $row['id']; ?>">Delete</a></td>
</tr>
<tr class="<?php echo $row['id'];?> datavalue" style="display: none;">
<form action="dashboard.php" method="post" id="updateform" class="<?php echo $row['id'];?> formclass">
<td><?php echo $srnumber;?></td>
<td><input type="hidden" name="upid" value="<?php echo $row['id'];?>">
<input type="text" name='upname' id="name" value="<?php echo $row['name'];?>"></td>
<td><input type="text" name='upemail' id="email" value="<?php echo $row['email'];?>"></td>
<td><input type="text" name='upaddress' id="address" value="<?php echo $row['address'];?>"></td>
<td><select name="upcity" id="city">
<option id="blankcity" value="blankcity"></option>
<option value="jalandhar" id="jalandhar" <?php if($row['city']=="jalandhar"){echo 'selected';} ?> >Jalandhar</option>
<option value="chandigarh" id="chandigarh" <?php if($row['city']=="chandigarh"){echo 'selected';} ?> >Chandigarh</option>
<option value="new_chandigarh" id="new_chandigarh" <?php if($row['city']=="new_chandigarh"){echo 'selected';} ?>>New Chandigarh</option>
<option value="panchkula" id="panchkula" <?php if($row['city']=="panchkula"){echo 'selected';} ?> >Panchkula</option>
<option value="sirsa" id="sirsa" <?php if($row['city']=="sirsa"){echo 'selected';} ?> >Sirsa</option>
<option value="mohali" id="mohali" <?php if($row['city']=="mohali"){echo 'selected';} ?> >Mohali</option>
</select></td>
<td><select name="upstate" id="state">
<option id="blankstate" value="blankstate" ></option>
<option value="punjab" id="punjab" <?php if($row['state']=="punjab"){echo 'selected';} ?>>Punjab</option>
<option value="hariyana" id="hariyana" <?php if($row['state']=="hariyana"){echo 'selected';} ?>>Hariyana</option>
<option value="chandigarh" id="schandigarh" <?php if($row['state']=="chandigarh"){echo 'selected';} ?>>Chandigarh</option>
</select></td>
<td><input type="text" name='upphone' id="phone" value="<?php echo $row['phone'];?>"></td>
<td><input type="radio" name="upgender" value="male" id="male" <?php if($row['gender']=='male'){echo 'checked';} ?>>Male
<input type="radio" name="upgender" value="female" id="female" <?php if($row['gender']=='female'){echo 'checked';} ?>>female</td>
<td><a type="button" onclick="usersaveupdate(<?php echo $row['id']; ?>);" href="#" class="saveupdate" data-value="<?php echo $row['id']; ?>"> Save </a></td>
<td><a onclick="usercancelclick(<?php echo $row['id']; ?>);" href="#" class="cancel" data-value="<?php echo $row['id']; ?>">Cancel</a></td>
<td><a onclick="deleteuserform(<?php echo $row['id']; ?>);" href="#" class="delete" data-value="<?php echo $row['id']; ?>">Delete</a></td>
</form>
</tr>
</table>

how to uppercase multiple id (checkbox in php using javascript)

How to uppercase each j1 "click" then the f1 text change to uppercase, j2 "click" then the f2 text change to uppercase, et seq..
<table>
<form>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction()}"></td>
<td><input type="text" id="f<? echo $i ?>" value="Test"></td>
</tr>
<script>
function myFunction() {
document.getElementById("f"+<? echo $i ?>).value = document.getElementById("f"+<? echo $i ?>).value.toUpperCase();
}
</script>
<? endforeach; ?>
</form>
</table>
Pass the value of $i to the function and use it to identify the element. Try with -
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction('<? echo $i ?>')}"></td>
And define the function -
<script>
function myFunction(i) {
document.getElementById("f"+i).value = document.getElementById("f"+i).value.toUpperCase();
}
</script>
You dont have to put the function inside the loop.
for another option, you don't need to declare "id" attribute in input text element, you can use "name" attribute http://jsfiddle.net/j4080ytq/
<form>
<table>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" onclick="if(this.checked){f<? echo $i; ?>.value = f<? echo $i; ?>.value.toUpperCase();}"></td>
<td><input type="text" name="f<? echo $i ?>" value="Test"></td>
</tr>
<? endforeach; ?>
</table>
</form>

Javascript PHP/MySQL - Insert row value from database into textfield

Possibly very easy to do but I cannot work it out!
I'm trying to make a button which inserts row value pulled out from the database into a textfield.
<?php while ($row = mysql_fetch_array($query)) { ?> <tr>
<td><?php echo $row['mobile_number']; ?></td>
<td><input type="button" value="select" onclick="clickMe()" /></td>
</tr>
<?php } ?>
<input type="text" id="textfield" />
so when the button "select" is clicked, the textfield would get populated with the mobile number.
Many thanks for your help in advance.
Try this:
<script>
function clickMe(number) {
document.getElementById("textfield").value = number;
// or jQuery
$("#textfield").val(number);
}
</script>
<?php while ($row = mysql_fetch_array($query)) { ?> <tr>
<td><?php echo $row['mobile_number']; ?></td>
<td><input type="button" value="select" onclick="clickMe(<?php echo $row['mobile_number']; ?>)" /></td>
</tr>
<?php } ?>
<input type="text" id="textfield" />

Categories