1

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...

2 Answers 2

1

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:

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

2 Comments

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
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.
1

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.

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.