0

i got some editable textbox inside html table cells! Each textbox got a summit button. I want send value of textbox and id of textbox to javascript function so it makes ajax post request to php script and edit mysql row!

My problem is that i dont know how to pass textbox value and its corresponding id to javascript and change textbox button on response of ajax.Does my buttons need unique ids as well ? could any one show me how this can done.Thanks

 $table3.="<tr>";
 $table3.="<td>" . $row['ID'] ."(".$totalRows. ")</td>";
 $table3.="<td bgcolor=\"#FF0000\"><input type=\"text\" id =\"". $row['ID'] ."\" name=\"". $row['ID'] ."\" value=\"edit\"><button onclick='setNewValue(\"".$row['ID']."\")'>Edit this Row</button></td>";
 $table3.="<td>" . $row['imgPURL'] . "</td>";
 $table3.="<td>" . $row['imgUrl'] . "</td>";
 $table3.="<td>".$line."</td>";  echo "</tr>";

javascript :

<script>
function setNewValue(a,b) {
var button = $(event.target);  // get button here

var text = $("#"+a);
var textboxvalue = text.val();

//alert("inputId:"+a+"\nuInputValue:"+b);


    var url = "./edit.php";
    $.post(url, {
        "method": "post",
        "rowId": a,
        "rowValue": textboxvalue,

    }, function(data) 
       {

          var ajaxResponse = data;

        //alert("success"+ajaxResponse);

            if(ajaxResponse.indexOf("SuccessEdit")>=0)
            {
              //remote the edit button
               button.hide();
            } 
            else
            {
              //change caption of edit button to try again
             button.text("some text");
            }

      })
    }

</javascript>
1

5 Answers 5

1

pass the id of the input box through the function like setNewValue(23)

function setNewValue(id){

$("#"+id);

}

Thats it you can get all the values of the input box and

this works if name the submit button with an id or rather a class;

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for replies. Ivin Polo Sony could you tell me how i can pass the id of inputbox to the function ? I tried this but it gives me erros!<button onclick=\'setNewValue(\" . $row['ID' . \")\'>edit this row</button>. After passing id of inputbox how can access value of inputbox itself ?
@user1788736 you can simply pass the is through the function parameter like this <input type = "button" onclick=" setNewValue(<?php echo $row['id']; ?>)" /> thats it.
1

This line:

$table3.= "<td bgcolor=\"#FF0000\"><input type=\"text\" id =\"". $row['ID'] ."\" name=\"". $row['ID'] ."\" value=\"edit\"><button onclick=\'setNewValue()\'>Edit this Row</button></td>";

to:

$table3.= "<td bgcolor=\"#FF0000\"><input type=\"text\" id =\"". $row['ID'] ."\" name=\"". $row['ID'] ."\" value=\"edit\"><button onclick=\'setNewValue(" . $row['ID'] . ")\'>Edit this Row</button></td>";

Then in your js:

function setNewValue(id){
    var text = $("#"+id);
    var value = text.val();

}

update, hide button or change caption

function setNewValue(id){
    var button = $(event.target);  // get button here

     // change caption 
     button.text("some text");

     // hide 
     button.hide();

}

13 Comments

srain the value of textbox is dynamic and editable. in your example you just put it edit! how to pass whatever user put in the textbox to javascript. I removed ,\"edit\" part and i get lots of error!
thanks i was keep trying var textboxvalue = $('input[id=+a]').val(); and it was not working . but now i get both id and textbox value in javascript. Now how i can remove the button or change caption of button if depending on ajax response from ajax call? i want somehow user know that edit was successfull. or forexample how to change bgcolor of inputbox on ajax response ?
@user1788736, how is the issue going?
srain there are more then one button in the page i just want to change the one that was clicked if the result of ajax resposnse is SuccessEdit(if(ajaxResponse.indexOf("SuccessEdit")>=0))! do i need to provide each button with unique id as well?
in function setNewValue(), var button = $(event.target); get the button you clicked.
|
0
 $table3.="<tr>";
 $table3.="<td>" . $row['ID'] ."(".$totalRows. ")</td>";
 $table3.="<td bgcolor=\"#FF0000\"><input type=\"text\" id =\"". $row['ID'] ."\" name=\"". $row['ID'] ."\" value=\"edit\"><button onclick=\'setNewValue(this)\'>Edit this Row</button></td>";
 $table3.="<td>" . $row['imgPURL'] . "</td>";
 $table3.="<td>" . $row['imgUrl'] . "</td>";
 $table3.="<td>".$line."</td>";  echo "</tr>";

the 'this' will reffer to the button element that gets clicked.

function setNewValue(btn) {
   var cell = btn.parentNode.querySelector("input[type='text']");
   var input = cell.querySelector("input[type='text']");
   var inputId = input.id;

...

Comments

0

you can pass this like a normal js parameter ==>

<input type=text id = <?php echo $row['id'];  ?> onclick= setNewValue(<?php echo $row['id']; ?>);


    function setNewValue(id){

    var obj = $("#"+id);

    console.log(obj);

    }

Comments

0

keep in mind if he is using the element the default type is submit and his entire form will probably submit. add either type="button" or use event.preventDefault() on the JS event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.