0

I'm having an issue passing a JavaScript variable through a form correctly to a controller. I'm not sure how to do this since it's my first time working with JavaScript. Any help would be appreciated.

My code can be found below:

  <script>

   function findDoc(e){
      ...
      var ID = grid._data[rIndex].document_ID;
   }

   ...

   {title: Grab, template: "<form action = '<?php echo $exepath;?> docs/grab' method = 'POST'><input type = 'submit' value = 'ID'></form>", width: 90}

   </script>
  • Can you post more information? What part are you stuck at? Are you getting a specific error? – jamis0n Apr 07 '14 at 04:09
  • guessing the php echo? view source, what do yo see ? –  Apr 07 '14 at 04:10
  • I just want a regular button so that when I click it, it sends the ID to the controller so the controller can retrieve it. –  Apr 07 '14 at 04:24

2 Answers2

0

You can send javascript variables back to the server using one of two ways :

1)Ajax call to the server How to make an AJAX call without jQuery?

2)form POST input values.

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data

You can add or modify input fields using javascript and catch them in a $_POST array.

Community
  • 1
  • 1
Patrick
  • 3,289
  • 2
  • 18
  • 31
0

If you are attempting to concatenate the value of the ID JavaScript variable with the string in your code you will have to use JavaScript's string concatenation operator:

{title: Grab, template: "<form action = '<?php echo $exepath;?> docs/grab' method = 'POST'><input type = 'submit' value = '"+ID+"'></form>", width: 90}

Also, I don't think you want a space in your action URL between the PHP echo and docs/grab

Alex W
  • 37,233
  • 13
  • 109
  • 109
  • Yeah. It was mistype. I'm trying to send ID to my controller through the form but I'm unsure of how to do it. I just want a regular button so that when I click it, it sends the ID to the controller so the controller can retrieve it. –  Apr 07 '14 at 04:24
  • @Requiem you need an `input` element inside of your `
    ` tags with a value, otherwise when you click submit nothing will be sent.
    – Alex W Apr 07 '14 at 04:37
  • I thought that was what I was doing though unless there needs to be another input element. Not sure how I'd go about this. –  Apr 07 '14 at 04:41
  • @requiem The only input element you have is a submit button. You need a `` etc. – Alex W Apr 07 '14 at 13:00
  • If I enter an input however, this still does not appear to work. Would the ID not be getting evaluated correctly or is there something else wrong? –  Apr 07 '14 at 17:12
  • @Requiem you have to give it a `name` attribute. Also, don't forget you have to concatenate ID's value with the `+` operator – Alex W Apr 07 '14 at 17:26
  • Well if I try to do something like this, then I get errors due to the concatenation. –  Apr 07 '14 at 17:30
  • @Requiem Oh, right. You will have to populate it with JavaScript. `document.getElementsByName('id')[0].value = ID;` should work. You are correct that the concatenation will not work; I thought you were using JavaScript not HTML. – Alex W Apr 07 '14 at 17:44
  • And that line would have to go in my javascript function while in the html input tag my name='id' value='ID' correct? –  Apr 07 '14 at 17:54
  • @Requiem All of that is correct. But don't add `value='ID'` in your HTML, because that line of JavaScript from my comment above will set the `value` attribute. – Alex W Apr 07 '14 at 18:15