1

I have a select element which I need to retrieve the value of selected option and then set an array's index to that value. I need this to happen on page load and whenever select is updated.

It looks something like:

{{ Form::select('marketplace_id', $donation_options, '', array('data-smart-select', 'required', 'id' => 'marketplace_id')) }}

<span id="market_hint" class="help-block">{{ $marketplaces[0]['example'] }}</span>

The select ID is marketplace_id

The array is $marketplaces and it's a multi-dimension array with the second value always being ['example'] but the first value being the value of the select which is always a number such as 0,1,2,3,etc.

By current javascript can retrieve the value of the select:

 $( "#marketplace_id" ).val();

But I am not sure how to proceed to update the array inside the market_hint span tag. Is there a simple method with jQuery?

2
  • you either have output the whole php array into a javascript variable or output it into html data attributes or make an ajax call to get the value when select is changed Commented Jul 12, 2014 at 1:31
  • you can't update the array, PHP has already ran. You cannot do anything with server side code,variables,functions etc once the page is in the browser. You will need to either put the entire array into a javascript variable when the page is rendered in php or use ajax to poll the new value from the server Commented Jul 12, 2014 at 1:32

1 Answer 1

1

If you want to go the route of putting the whole array into javascript when php renders the page

?>
<script>
$(function(){
   var myData = <?php echo json_encode( $marketplaces ); ?>
   $("#marketplace_id").change(function(){
      var value = $(this).val();
      $("#market_hint").html(myData[value]["example"]);
   });
   //do a call here to do an initial setting of the span
   $("#marketplace_id").change();
});
</script>

Otherwise you will have to do an ajax request to a script that will give you the data

$(function(){
   $("#marketplace_id").change(function(){
      var value = $(this).val();
      $.get("someScript.php?theIndex="+value)
      .done(function(response){
          $("#market_hint").innerHTML = response;
      });
   });
});

someScript.php

<?php
$index = isset($_GET["theIndex"]) ? intval($_GET["theIndex"]) : 0;

//do some processing to get the needed data
//...

echo $data;
die;

Of course tailor it to fit in with your laravel framework, just used plain php as I do not overly know laravel

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

3 Comments

need to change using innerHTHL as jQuery method, will throw error
Thanks for the help so far. My only other questions is how would I get the JS to show on load as well? right now you need to select a different select option for the spam to update. Here is live demo: jsfiddle.net/aQ4vT/1
@MCG, I just took it that you would just keep the laravel template in place for the initial span text. If you want the initial value to be shown by javascript you can just add a $("#marketplace_id").change() call right after setting the event listener.

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.