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

  • Home
  • SEARCH
  • 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 8620009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:28:41+00:00 2026-06-12T06:28:41+00:00

Based on several other threads here I think what I’m looking for is json_encode(),

  • 0

Based on several other threads here I think what I’m looking for is json_encode(), though I don’t really understand how the output can be used by my JavaScript function. What I’m trying to do is allow users to add additional select lists as needed and have those select options populated from a PHP function that I use to create the initial select list. Here’s my JavaScript at the moment:

<script type="text/javascript">
var assigneeCounter = <?php print checkdata("assignee_count", $formvalues, "1"); ?>;

function addInput(fieldlabel, inputclasses, inputCounter, fieldtype = 'input', selectoptions = false){
  window[inputCounter]++;
  var cleanlabel = fieldlabel.replace(/[^a-zA-Z 0-9]+/g,'');
  var cleanlabel = cleanlabel.replace(/\s+/g, '_').toLowerCase();
  var newdiv = document.createElement('div');
  newdiv.id = cleanlabel + window[inputCounter];
  if (fieldtype == 'input') {
    newdiv.innerHTML = '<div class="form-item"><label for="' + cleanlabel + '_' + window[inputCounter] + '">' + fieldlabel + ' #' + window[inputCounter] + '</label><input type="text" name="' + cleanlabel + '_' + window[inputCounter] + '" id="' + cleanlabel + '_' + window[inputCounter] + '" value="" class="' + inputclasses + '"><span class="space"><a href="javascript:removeInput(\'' + cleanlabel + '\',\'' + inputCounter + '\',\'' + window[inputCounter] + '\');">Remove</a></span>';
  }
  else if (fieldtype == 'select') {
    alert(selectoptions);
    newdiv.innerHTML = '<div class="form-item"><label for="' + cleanlabel + '_' + window[inputCounter] + '">' + fieldlabel + ' #' + window[inputCounter] + '</label><select name="' + cleanlabel + '_' + window[inputCounter] + '" id="' + cleanlabel + '_' + window[inputCounter] + '" class="' + inputclasses + '">
      <span class="space"><a href="javascript:removeInput(\'' + cleanlabel + '\',\'' + inputCounter + '\',\'' + window[inputCounter] + '\');">Remove</a></span>';
  }
  document.getElementById(cleanlabel + "_additions").appendChild(newdiv);
  document.getElementById(cleanlabel + "_count").value = window[inputCounter];
}
function removeInput(fieldlabel, inputCounter, fieldID){
  var element = document.getElementById(fieldlabel + fieldID);
  document.getElementById(fieldlabel + "_additions").removeChild(element);
  window[inputCounter]--;
  document.getElementById(fieldlabel + "_count").value = window[inputCounter];
}
</script>

I’m calling the function in HTML like so:

<div id="assignee_additions"></div>
<p><a href="javascript:addInput('Assignee','required-text', 'assigneeCounter', 'select', '<?php print json_encode($assignto); ?>');">Add Another Assignee</a></p>

When I set the value of “fieldtype” to input it works great and creates another input field as expected. The remove function does its job too. But what I really need is a select field, not an input field and that’s where my lack of JavaScript knowledge is killing me.

Also, if anyone seems to think that jQuery is a better option here, I’m certainly open to that as well but I don’t know that any better than I do traditional JavaScript.

Thanks in advance!

  • 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-12T06:28:42+00:00Added an answer on June 12, 2026 at 6:28 am

    You can create your JSON in a different php file. Then, from your javascript function, you can use an AJAX call to retrieve generated JSON.

    I believe you want to json_encode values and keys for <option> tags, right? So, after successful AJAX call, decode your JSON and for each value build a new option.

    Rough code in jQuery can look like this:

    $.ajax({
        url: 'giveMeJSON.php',
        dataType: 'json',
        success: function(data){
            $.each(data.options, function(index){
                $('<option>').val($(this).value).text($(this).text).appendTo($('#select_id'));
            }
        }
    })
    

    Read about $.ajax and AJAX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm modeling a force-based physics simulation where several particles are interacting with each other.
I've looked through several other posts similar to this but can't find anything that
Me and several other developers are currently cleaning up our legacy code base, mostly
In my XNA game, I needed to define an irregular shape based on several
I ever developed several projects based on python framework Django. And it greatly improved
We've got several web-based applications that are launched from our ERP system (SAP R/3
I am populating several fields based on data from the my db. I am
I have several text fields that are dynamically populated based on user choice from
We have build several web service based on .net. Now we want to create
I'm storing several versions of a file based on a digest of the original

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.