Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:26:27+00:00 2026-06-03T11:26:27+00:00

I have a JavaScript function (which users on this forum graciously helped me construct)

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:26:30+00:00Added an answer on June 3, 2026 at 11:26 am

    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:

    • jQuery Data
    • jQuery .on()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript function which redirect to Asp.net MVC action method. [AcceptVerbs(HttpVerbs.Post)] public
I have a javascript function which should populate a select box with all items
I have a JavaScript function which is quite long and performs a number of
I have a javascript function which needs to do a numerical calculation. Some of
I have a JavaScript function buildTable which builds a very long HTML table. function
Everybody, I have a one HTML page which contain one Javascript function which shows
I have a javascript function for checking errors which I am calling on OnClicentClick
I would like a JavaScript function to have optional arguments which I set a
I have a Javascript variable which I am setting a PHP variable to. function
I've had some experience with Java and Javascript and searching this forum has helped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.