2

I’m looking to be able to edit an individual line in a php loop. I have this script here that I think is almost perfect, except I don’t know how to add the Value to the “PriorityForm”.

<script type="text/jscript">
function PriorityInfo(Value) {
      $.post("updatenote.php?ID="+Value, { Note: PriorityForm.Note.value},
              function(output) {
                   $("#Priority"+Value).html(output).show();
                   });
      }
</script>

The (Value) in “PriorityInfo(Value)” changes. I’ve gotten the Value to add to everything but “PriorityForm”. For example, { Note: PriorityForm.Note.value} needs to be { Note: PriorityForm9.Note.value} if the Value is 9. I’ve tried

Note: PriorityForm+ Value.Note.value
Note: PriorityForm+ (Value).Note.value
Note: PriorityForm+ “Value”.Note.value
Note: “PriorityForm”+ Value.Note.value

This cause the JavaScript not to run so I’m pretty sure it’s a syntax error. My mistake, here's the PHP code:

   <?php
   $info0 = "SELECT * FROM CT:GTMQuestionPoints WHERE Question = 'Priority' AND PointLossNote NOT LIKE ''";
    $rs0=odbc_exec($connq,$info0);
    while($row = odbc_fetch_array($rs0)) {
    $ID = "" . $row["ID"] . "";
    $Note = "" . $row["PointLossNote"] . "";
    $Points = "" . $row["Points"] . "";
    echo '
    <table>
    <tr>
    <td style="width: 700px"><form name="PriorityForm'.$ID.'"><input name="Note" type="text" value ="'.$Note.'" style="width: 700px"></td>
    <td style="width: 100px"><input name="Points" type="text" value ="' . $Points . '" style="width: 100px"></td></form>
    <td style="width: 180px"><input name="updatepriority" type="button" value="Update" onclick="PriorityInfo(&#39;'.$ID.'&#39;)"><div id="Priority'.$ID.'"></div></td>
    <td style="width: 180px"><form method="POST" action="rmsdeletenote.php?Priority=' . $Note . '"><input name="modify" type="submit" value="Remove"></td></form>
    </tr>
    </table>
    ';
    }
    ?>
3
  • Use an array or object instead of variable variables. See stackoverflow.com/q/11770474/218196 and many others: stackoverflow.com/… Commented May 13, 2014 at 18:17
  • What PHP loop? If you want to ask a question about PHP, post your PHP code. Commented May 13, 2014 at 18:17
  • What is PriorityForm? Where is it declared, what value does it contain? Commented May 13, 2014 at 18:18

2 Answers 2

1

Try this: Step one, PHP on the primary page,

<?php
;//extend the form around the loop, and get rid of the form for the remove button (replacing with javascript)//
echo '<form name="PriorityForm">';

$info0 = "SELECT * FROM CT:GTMQuestionPoints WHERE Question = 'Priority' AND PointLossNote NOT LIKE ''";
$rs0=odbc_exec($connq,$info0);

while($row = odbc_fetch_array($rs0)) {
$ID = "" . $row["ID"] . "";
$Note = "" . $row["PointLossNote"] . "";
$Points = "" . $row["Points"] . "";
//give each field name a unique name//
echo '      
<table>
<tr>
<td style="width: 700px"><input name="Note'.$ID.'" type="text" value ="'.$Note.'" style="width: 700px"></td>
<td style="width: 100px"><input name="Points'.$ID.'" type="text" value ="' . $Points . '" style="width: 100px"></td>
<td style="width: 180px"><input name="updatepriority" type="button" value="Update" onclick="PriorityInfo(&#39;'.$ID.'&#39;)"><div id="Priority'.$ID.'"></div></td>
<td style="width: 180px"><input name="modify" type="button" value="Remove" onclick="Remove(&#39;'.$ID.'&#39;)">

</tr>
</table>
';
}
echo '</form>';
?>

Step two: JavaScript on the primary page,

<!-- send the specific ID and serialize everything else to the updatenote.php page-->
<script type="text/javascript">
function PriorityInfo(Value) {
$.post("updatenote.php?ID="+Value, 
 $('form[name="PriorityForm"]').serialize(), 
 function (output) {
 $("#Priority"+Value).html(output).show();
});
}
</script>
<!-- send the specific ID to the deletenote.php page-->
<script type="text/jscript">
function Remove(Value) {
location.href = "deletenote.php?ID="+Value;
}
</script>

Step three: Additional/replacement php code for updatenote.php,

// get the ID//
If(!empty($_REQUEST['ID'])){$ID = ($_REQUEST['ID']);}

//add field name and ID to isolate the correct one//
$Note = "Note" . $ID;
$Points = "Points" . $ID;

//then retrieve only the one with the ID
If(!empty($_POST[$Note])){$NoteResult = $_POST[$Note];}
If(!empty($_POST[$Points])){$PointsResult = $_POST[$Points];}

echo $ID;
echo '<br>';
echo $NoteResult;
echo '<br>';
echo $PointsResult;

This may not be the cleanest way to do it, but it works for me.

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

Comments

0

Looking at your POST argument, "{ Note: PriorityForm.Note.value}", you're using PriorityForm as though it's an object. But based on the code you provided, you don't have a "PriorityForm" variable declared anywhere. So instead, you might want to consider fetching the value from the text input named "Note" using jQuery. There are a number of ways to do this. Here's one:

<script type="text/javascript">
function PriorityInfo(Value) {
    var args = new Object();
    args["Note"] = $("input[name='Note']").value;
    $.post("updatenote.php?ID="+Value, args, 
        function(output){
            //other code here   
        });
}
</script>

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.