Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JavaScript function (which users on this forum graciously helped me construct) that passes variables via POST to a PHP file with a query for inserting the data to a MySQL database.

The function is invoked "onchange" for a series of 2000+ rows that are spit out from a MySQL database. I use the ID of the row to give each form field a unique name/id, like this:

echo "=<select name='$AdvertisersSQLResult[id]geolocation' id='$AdvertisersSQLResult[id]geolocation' onchange = 'insertAdvertiser()'>"; 

The JavaScript function looks like this:

function insertAdvertiser() {
var data = $('#<?php echo $AdvertisersSQLResult[id]?>dataid,#<?php echo $AdvertisersSQLResult[id]?>industry,#<?php echo $AdvertisersSQLResult[id]?>geolocation').serialize();
$.post('/database/InsertAdvertiserRelationship2.php', data);
return false;
}

As you can see, I'm attempting to pass PHP variables as part of the form id values. However, this doesn't work, as the function is written to the page once (in the section) without any variables yet populated.

In other words, I'd like to have this JavaScript function utilize whatever PHP variable is being passed to it dynamically. I've looked at other threads about passing a PHP variable to a JavaScript function, but I can't find any reference to how this can be done dynamically, such that the JavaScript function changes to use a different PHP variable each time (if that makes sense).

Thanks for any help...

share|improve this question

2 Answers

up vote 1 down vote accepted

Well yes because you'd need a seperate insertAdvertiser() method for every advertiser on the system.

A better way to do it would be to do this: Javascript:

// This line turns your PHP array into a Javascript object
var advertisers = <?php echo json_encode($AdvertiserSQLResult); ?>; 

function insertAdvertiser(id) {
 $.post('/database/InsertAdvertiserRelationship2.php', advertisers[id]);
}

// Try not to use attributes to bind events, use handlers like this instead.
$(document).ready(function() {
 $('select').on('change', function() {
   // Reads the advertiser id from the data attribute
   insertAdvertiser($(this).data('advertiserid'));
 });
});

HTML:

<select data-advertiserid="<?php echo $AdvertiserSQLResult['id']; ?> class="advertiser-select">...</select>

I wrote this as I went along so apologies if I've overlooked something.

See:

share|improve this answer
You're awesome. Thanks much – Ben Wilson Apr 30 '12 at 19:21
Actually, sorry, I'm a bit lost... so I put the HTML you've referenced inside my PHP while loop as I'm spooling out the form, correct? The ID is actually intended to be a hidden field that doesn't appear in the form. So I would use $AdvertiserSQLResult['industry'] and $AdvertiserSQLResult['geolocation'] for the select menus I want input for? Also, how do I sent multiple data values (i.e. industry and geolocation) through this function? Thanks again – Ben Wilson Apr 30 '12 at 20:14
Also, I'm not getting any POST response in Firebug when I send this to my PHP script (InsertAdvertiserRelationship2.php). How do I reference the data in that file? Thanks again. – Ben Wilson Apr 30 '12 at 20:38

You can make a global JavaScript variable and use it in your scripts.

<script type="text/javascript">
   myVariable = <?php echo $x; ?>;
</script>

You can store your variables someplace and then access them via JavaScript.

<input type="hidden" class="myVariable" value="<?php echo $x; ?>"

<script type="text/javascript">
   var myVar = $('.myVariable').val();
</script>

After you have your data in JavaScript you can do what ever you want to do with it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.