new2programming
new2programming

Reputation: 257

Issue passing PHP variables through Javascript and back

so this function seems to be confusing me.

    echo"
<td style='font-size:12px;width:150px;'><div style=\"overflow-y:auto; max-height:250px; width:200px;\">
{$row['Notes']} </div><br /><center><br />

    <button onclick=\"myFunction('{$row['ID']}','$rowID')\">Add Note</button>



<form action=\"http://calls.fantomworks.com/functions/notes.php\" id='notesForm' name='notesForm' method='post'>
    <input type='hidden' id='notesID' name='notesID' />
    <input type='hidden' id='rowID' name='rowID'/>
    <input type='hidden' id='notes' name='notes' />
    </form>


</center>";

Calls this javascript

<script language="JavaScript" type="text/javascript">
    function myFunction(ID,rowID) 
    {
        var x;
        var ID = ID;
        var rowID = rowID;
        var note = prompt("Customer Note","Write your customer note here...");

        if (note != null) {
            document.getElementById("notes").value = note;
            document.getElementById("notesID").value = ID;
            document.getElementById("rowID").value = rowID;
            document.getElementById("notesForm").submit();
        } 
    else{
       return false;
        }
    }
</script>

and ends up at this php page

$notesID = $_POST['notesID'];
$rowID = $_POST['rowID'];
$note = $_POST['notes'];
//Redirect to browser
header("Location: ./index.php#row_$rowID");

The only problem is that the rowID does not seem to be making it through and generates links ending like "index.php#row_"

I can't make sense of why rowID isn't coming through but NotesID and notes are. As you can see from the debug the value is there. enter image description here

Thanks for any thoughts or suggestions!!

Upvotes: 1

Views: 67

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33813

The script at "http://calls.fantomworks.com/index.php" is being POSTed to by your javascript function - thus the variable that you seek ought to be available through the $_POST global.

Try changing

header("Location: ./index.php#row_$rowID");

To

header("Location: ./index.php#row_{$_POST['rowID']}");

Incidentally, the three variables you define in the javascript function seem redundant and could be removed by the looks of things, namely:-

var x;
var ID = ID;
var rowID = rowID;

Have had a closer look since posting original ( and hadn't noticed the assignment of posted vars by the @OP ) - there are hundreds of forms on the page in question - same IDS used from row to row to row. IMHO - this is definitely NOT the way forward - You could have just one form for "Add Note" as you dynamcally set the value by clicking the button. It does appear that the relevant vars ( rowID etc ) are being set and assigned to the button that calls the javascript so theoretically you could have just one form that is used to post to "notes.php" but have this button on each row.

In terms of a general critique / suggestions

The page is very slow to load - due in part to there being hundreds of complex table row layouts, and by the looks of things a form for every button - then there are the images which themselves are fullsize but could really be ( and should be ) thumbnails. The number of forms could be drastically reduced if each button were to dynamically assign the variables like the one in the question above.

Upvotes: 2

Related Questions