tags:

views:

79

answers:

3

In the below code i have an array arrSGoal. On Click of RemoveGoal i need to delete or set the value to 0 for the the id in the array. Array is in php. dont know how to use in the jquery.

<SCRIPT LANGUAGE="JavaScript">
$(document).ready(function() {
$('a.removegoal').click(function(e)
    {
    e.preventDefault();
    //Getting the TR to remove it in case of successful deletion
    var objRow = $(this).closest('tr');
        objRow.remove();            
});

});


$arrSGoal[$i] = $row->id_goals; 
?>

<tr>
  <td style='vertical-align:top;'>
   <textarea name="stg<?php print $i;?>" id="short_goal" class="short_go"><?php print   $row->goal_description?></textarea>

</td> < td style='vertical-align:bottom;' nowrap> <span class='hidden'> echo $i </span> <a href=# class="removegoal" >Remove Goal

A: 

I don't understand your code, but a good way to "export" an array from PHP into Javascript is json_encode().

At any rate, you need to be aware that PHP is executed server side, and Javascript on the client side. There is no way to directly interact between the PHP code and Javascript, e.g. to do a calculation in JQuery and have PHP work with the result. (Well, without AJAX, that is).

Pekka
It worked. var arrayFromPHP = <?php echo json_encode($arrSGoal); ?>;$.each(arrayFromPHP, function (i, elem) { alert(elem);alert(i);});
ASD
I worked around the jscon_encoded array. how i can get the modified array back to php
ASD
The only way to get that array back is Ajax: http://docs.jquery.com/Ajax
Pekka
A: 

You can echo php variables in jquery/javascript, something like this:

alert('Your score is <?php echo "$variable" ?> . Thank you for playing.');
Sarfraz
+1  A: 

Once your HTML page has been served up the PHP is "dead" - its already been executed and completed and no longer exists. Its output was your HTML page -- the PHP is gone.

In order for you to do this you'd either have to have an AJAX call to the server and provide it what the information you wish to update, or have the link submit a form with that info, and do it (again) server side.

PHP doesn't actually run in your browser.

Erik