0

In my javascript I am trying to pass a php variable through the html name tag. The html is appended to my selected options.

$("#a"+$selected.val()).append('<input type="hidden" name="<?php echo $event_id; ?>" value="'+$selected.text()+'">');

Here is the entire js function to see what is going on http://laravel.io/bin/Edz25

Now, I know for a fact and have tested in my view that $event_id is returning an array by doing the following in my controller and also echoing the variable in the view.

print_r($event_id);
die();

Controller:

$data['event_id'] = $this->model_location->get_event_id();

Model:

public function get_event_id()
{

    $this->db->select('id');
    $query = $this->db->get('table_eventcards');

    return $query->result_array();


}

Now I am not sure why the variable in my javascript is not outputting anything.

Here is the output when I inspect the element.

<li data-value="28" id="a28">Event title<input type="hidden" name="<?php echo $event_id; ?>" value="Event title"></li>

If this helps this is my console log http://postimg.org/image/owtz3v8h7/

I really need to get this solved and appreciate all help.

17
  • "$ is not defined". Do you load jQuery BEFORE you run your script? Commented Sep 30, 2014 at 19:47
  • 1
    Are you sure that that page is being processed by PHP? Commented Sep 30, 2014 at 19:48
  • @lesssugar The script is in a carabiner loaded at the footer of every page which loads the jquery sources. If all my other jquery is working I do not see this as an issue that would effect the php variable. Commented Sep 30, 2014 at 19:50
  • 1
    "I know for a fact that $event_id is returning an array", and how do you expect to use an array as a name ? Commented Sep 30, 2014 at 19:51
  • You are using a separated JS file or <script> tags for javascript? Commented Sep 30, 2014 at 19:51

2 Answers 2

1

The .js files included in the HTML are not parsed by PHP, for do this you can use < script> tags and write this variables, example:

First, write your variable in the php file:

mypage.php

 <script>
     var MY_VAR = "<?php echo $myVar; ?>";
 <script>
 <script type="text/javascript" src="my_script.js"></script>

And then you can use this variable on your js file:

my_script.js

$("#a"+$selected.val()).append('<input type="hidden" name="' + MY_VAR + '" value="'+$selected.text()+'">');

A tested example:

<?php
     $myVar = 1;
 ?>

 <script>
     var MY_VAR = "<?php echo $myVar; ?>";
     alert(MY_VAR);
 </script>

The alert output will be "1"

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

2 Comments

I added the script tag on the php page. Then added the js variable in my .js page. It still give the same output treating the php variable as a string.
The js variable set to php variable in view is passing to .js file. However, strangely the alert is not working on the same view.
0

CodeIgniter's result_array() will return a multidimensional array.

Your ActiveRecord query doesn't appear to include any filtration (no WHERE clause), so there is the possibility that several rows will be returned.

Even if there is only one row in the table, the result set will be like:

array (
    array(
        'id' => 1
    )
)

Ergo, $data['event_id'] (in the contoller) AND $event_id (in the view) will contain the above data structure.

Some advice on variable naming... if a variable contains more than one of something (aka isIterable), then use a plural variable name so that you aren't confusing future developers of your script (the next developer might actually be you).

Adding to the confusion, you seem to be trying to assign the hidden input's name as an array -- I really don't understand the intent here.

If you want a specific id, you need to pull it from the array. If you want to iterate the array of ids, then you will need to adjust your js process.

Either way, to pass the data from server-side (php) to client-side (javascript), you will have stable success in the long term, if you use json_encode() (avoiding quoting and quote escaping issues).

<script>
let event_id = <?php echo json_encode($event_id); ?>;
</script>

Then makes sure that the external script that will use event_id is loaded AFTER the view renders the above javascript declaration.

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.