insert and delete functionality with ajax - php

I am working on basic functionality of insert,add,delete and update using ajax.
I have done little functionality write now..actually i want all functionality in a grid .
I put all html code inside a for loop.. this loop will execute till total num rows.. first execution of loop is fine, problem occur after this... next time when i click on delete button ajax call is not working.
Need help on this....
I have 2 files.. form.php and operation.php
code of form.php..
$(document).ready(function(){
$("#delete").click(function(){
var id=$("#uid").val();
$.post("operation.php",{ID:id},function(data){
$("#result").html(data);
});
});
});
for($i=0;$i<2;$i++)
{
echo "<input type='text' value=".mysql_result($all_records, $i, "id")." name='uid' id='uid'>
<input type='text' value=".mysql_result($all_records, $i,"name")." name='name' id='email' placeholder='Email'>
<input type='text' value=".mysql_result($all_records, $i,"email")." name='email' id='email' placeholder='Email'>
<input type='password' value=".mysql_result($all_records, $i,"password")." name='pass' id='pass' placeholder='Password'>
<input type='submit'name='delete' id='delete' value='Delete'>
<input type='submit'name='update' id='update' value='Update'>";
echo "</td></tr>";
}
operation.php
write now i put only one line code for checking purpose...
echo $_POST['ID'];

Well i think you know that ids should have unique names. It is not a good idea to give same id to multiple elements in the same page.
I think the code bellow will solve your purpose.
$(document).ready(function(){
$(".delete").click(function(){
var id=$(this).data('id');
$.post("operation.php",{ID:id},function(data){
$("#result").html(data);
});
});
});
for($i=0; $i<2; $i++)
{
echo "<input type='text' value='".mysql_result($all_records, $i, "id")."' name='uid' class='uid'>
<input type='text' value='".mysql_result($all_records, $i,"name")."' name='name' class='name' placeholder='Email'>
<input type='text' value='".mysql_result($all_records, $i,"email")."' name='email' class='email' placeholder='Email'>
<input type='password' value='".mysql_result($all_records, $i,"password")."' name='pass' class='pass' placeholder='Password'>
<input type='button'name='delete' data-id='".mysql_result($all_records, $i, "id")."' class='delete' value='Delete'>
<input type='submit' name='update' class='update' value='Update'>";
echo "</td></tr>";
}
Try to understand it and compare it with your previous code.
Let me know if you need further assistance.

Related

jquery - change raido button selection on input

i have this code:
Amount<input type='text' name='amount' size='10' >
<br>
Free<input type='text' name='fees' size='10' >
<br>
------------
<br>
<input type='radio' name='amount_type' value='Receive'checked> Receive<br>
<input type='radio' name='amount_type' value='Send'> Send<br>
-------------
<br>
<input type='radio' name='curr' value='LBP'checked> LBP<br>
<input type='radio' name='curr' value='USD'> USD<br>
what i want to do is
if amount more than 5000 , check radio button curr : value LBP else check usd
if fees are inserted , in radio button amount_type : check Receive else check send.
can you assist please
Use this code.
Amount<input type='text' name='amount' size='10' id='amount' >
<br>
Free<input type='text' name='fees' size='10' id='fees' >
<br>
------------
<br>
<input type='radio' id='receive' name='amount_type' value='Receive'checked> Receive<br>
<input type='radio' id='send' name='amount_type' value='Send'> Send<br>
-------------
<br>
<input type='radio' id='lbp' name='curr' value='LBP'checked> LBP<br>
<input type='radio' id='usd' name='curr' value='USD'> USD<br>
jquery
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("#usd").prop("checked", true);
$("#send").prop("checked", true);
$("#amount").on("input", function(){
// Print entered value in a div box
if($("#amount").val() > 5000){
$("#lbp").prop("checked", true);
}else{
$("#usd").prop("checked", true);
}
});
$("#fees").on("input", function(){
// Print entered value in a div box
if($("#fees").val() > 0){
$("#receive").prop("checked", true);
}else{
$("#send").prop("checked", true);
}
});
});
</script>
check out online demo : DEMO

PHP: Multiple forms aren't working except the first one?

This code generates multiple form associated with each product and its id.
But at html side only first is working. When I inspected the page I came to know that this code only generates form for first product only. Anyone Else faces this?
for ( $b = 0; $b < sizeof( $id ); $b++ ) {
echo "
<form action='Post.php' method='GET'>
<div class='form-group' style='display:none' id='$id[$b]'>
<label class='control-label'>Message</label>
<input type='text' name='id' value='$id[$b]'style='display:none'>
<input type='text' name='nam' value='admin 'style='display:none'>
<textarea type='text' class='form-control ' rows='4' col='10' name='mess' >
</textarea>
<input style='margin-top:10px' type='submit' class='btn btn-info' value='Submit'>
</div>
</from>";
}
You seem to close your form tag with 'from'.
change /from to /form

php post not able to grap element which append by using jquery

here is my code, hope you all can understand.
[html form coding]
<form method='post' action='pro/handle.php'><br/>
<input type='text' name='FirstTXT' value='First'>
<button type='button' name='ADDINPUT'>add</button>
<div id='SETSET'>
<!-- prepare for adding input element by using jquery -->
</div>
<input type='text' name='LastTXT' value='Last'>
<button type='submit' name='BTN' value='Submit'>submit</button>
</form>
[Jquery coding]
$(document).on("click","[name=ADDINPUT]",function(){
var inp = "<input type='text' name='ADDTXT'>";
$("#SETSET").append(inp);
});
So far NO error/problem for the code above(tested), jquery input did append into #SETSET accordingly.[The problem is]
after click on form [submit] button, php NOT able to get the element of [ADDTXT]when i use php to loop & get everything from previous page, result as below:-
[PHP coding]
foreach($_post as $key=>$val){
echo "<br/>$key : $val";
}
[Result]
FirstTXT : First<br/>
LastTXT : Last<br/>
BTN : Submit<br/>
//=== Missing ADDTXT (between First & Last)===//
Did you guys/gals know why?
Maybe that:
html:
<form method='post' action='pro/handle.php'><br/>
<input type='text' name='FirstTXT' value='First'>
<button type='button' name='ADDINPUT'>add</button>
<div id='SETSET' style='display: none;'>
<!-- prepare for adding input element by using jquery -->
<input type='text' id='ADDTXT' name='ADDTXT' disabled='disabled'>
</div>
<input type='text' name='LastTXT' value='Last'>
<button type='submit' name='BTN' value='Submit'>submit</button>
</form>
and JS:
$(document).on("click","[name=ADDINPUT]",function(){
$("#SETSET").show();
$("#ADDTXT").removeAttr("disabled");
});
You have an array from that field.
Replace:
var inp = "<input type='text' name='ADDTXT'>";
With:
var inp = "<input type='text' name='ADDTXT[]'>";
And PHP:
if(is_array($val)) { print_r($val); }
OR try:
var no = 0;
$(document).on("click","[name=ADDINPUT]",function(){
++no;
var inp = "<input type='text' name='ADDTXT"+ no +"'>";
$("#SETSET").append(inp);
});

Jquery Radio Button Submit Form

I have multiple forms on one page all are like this: The ID value changes but the options can be similar.
<form class="test"><td>4000</td>
<td align='center'><input type='radio' name='radio' value='car' checked></td>
<td align='center'><input type='radio' name='radio' value='boat' ></td>
<td align='center'><input type='radio' name='radio' value='Plane' ></td>
<input type='hidden' name='id' value='4000'/>
<input type='hidden' name='location' value='local'/>
<div class="form" style="display: none;">
</div></form>
Using JQuery I'm trying to submit this when any of the 3 radio buttons are selected.
<script>
$("input[name='radio']").click(function() {
var CurrectForm=$(this).parents('.test:first');
$.post(
'do.php',
CurrectForm.serialize(),
function( response ) {
CurrectForm.find('#form').html( response );
show( response );
}
);
} );
</script>
I've used similar to the above with checkboxes and dropdown lists, which have worked fine. But this doesn't seem to submit the values to the do.php page. The values that are submitted are show in the div form. Doing a var_dump($_POST); results in an empty array.
Can someone point me in the right direction ?
Thanks
Changing the form ti this seems to work ?
<td><form class="test">
<input type='radio' name='radio' value='car' checked>
<input type='radio' name='radio' value='boat' >
<input type='radio' name='radio' value='Plane' >
<input type='hidden' name='id' value='4000'/>
<input type='hidden' name='location' value='local'/>
<div class="form" style="display: none;">
</div></form></td>
Why does changing the cause it to work ?
Jfiddle with working example :) http://jsfiddle.net/QvpH5/
My guess is that there are multiple forms on your page with the class test. What is happening, is when you use parents(), you don't realize it first finds all of the ancestors, and then searches for .test:first. This will return the very first form's values, regardless of which form is clicked.
Look at this jsFiddle to see the fix (change .parents to .parent): http://jsfiddle.net/SDzzr/

Pass a php variable to dynamically generated div's onclick event

how to pass a php variable to dynamicaly generated div's onclick event?
here is my code
while($rowset = mysql_fetch_array($result))
{
$idTT1++;
$editTT='b'.$idTT1;
$hidTextTT='t'.$idTT1; $hidSubIdTT='s'.$idTT1;
echo "<tr><td>".$rowset[1]." ".$rowset[2]."</td><td> </td>
<td class='table_label'>
<div id='".$idTT1."' onclick='editView($idTT1,$editTT)' >".$rowset[3]."</div>";
echo "<div id='".$editTT."' style='display:none;'>
<input id='".$hidSubIdTT."' name='box1' type='hidden' value='".$row['dDegreeName']."'>
<input id='".$hidTextTT."' name='box2' type='text' value='' />
<input type='submit' value='Update' name='submit'
onclick='updateSubject($hidSubIdTT,$hidTextTT)'/>
<input type='button' value='Cancel' name='Cancel' onclick='setEditView($idTT1,$editTT)'/>
</div></td></tr>";
}
i want to pass 2 variables $idTT1 and $editTT. im getting the value of $editTT1 in javascript but i cant get value of $editTT value in editView() javascript function.
That is probably because you are not quoting the IDs. Try
setEditView(\"$idTT1\",\"$editTT\")
in the line before last.
You need to put it between quotes. Something like this should work:
"<input type='button' value='Cancel' name='Cancel' onclick='setEditView(\"$idTT1\",\"$editTT\")'/>"

Categories