bmo
bmo

Reputation: 13

hidden variable not sent to php file

I am trying to send hidden variables to a php file. For soe reason the php does not receive these variables. Below is the code aimed at sending these vars. Any idea on why it does not work? Thanks for your help.

   ////// For the form of training data
  // bind 'myForm1' and provide a simple callback function
       $('.myForm1').ajaxForm(options);

  // attach handler to form's submit event
       $('.myForm1').submit(function() {       // .myForm1 is a class so applies to all forms of this page

  // passing PK/FK as hidden value to php (drop down menu displays values, here I get the PK-PF via key and send them to php for upload)
  var sown=$('#selpersonO').find(":selected").attr('key');  // finds the value of key of the selected choice in the drop down menu. Key is sent back by the php code that send info to drop down menu
             //console.log(tmp);
             //console.log(this);
            $(this).append('<input/>').attr('type','hidden').attr('selpersonO',sown);  // makes key value as hidden and passed to the php form that uploads data
            //console.log( $('.myForm')) ;
  var saccess=$('#selaccess').find(":selected").attr('key');
            $(this).append('<input/>').attr('type','hidden').attr('selaccess',saccess);
  var scountry=$('#selcountry').find(":selected").attr('key');
            $(this).append('<input/>').attr('type','hidden').attr('selcountry',scountry);
  var sprod=$('#selpersonP').find(":selected").attr('key');
            $(this).append('<input/>').attr('type','hidden').attr('selpersonP',sprod);

  // submit $the form
     $(this).ajaxSubmit();
  // return false to prevent normal browser submit and page navigation
     return false;
     });

Upvotes: 0

Views: 205

Answers (2)

David Fregoli
David Fregoli

Reputation: 3407

In your code:

$(this).append('<input/>').attr('type','hidden').attr('selpersonO',sown);

the .attr() are set for the form element (or whatever $(this) referes to), not the input element; .append() returns the element which you append to, not the newly created and appended.

Use this notation to create nodes and set their attributes:

$(this).append(
    $('<input>', {
         type : 'hidden',
         selpersonO : sown,
         name : 'required', // YOU NEED TO SET NAME AND VALUE
         value : 'required' // FOR PHP TO RETRIEVE THEM
    })
);

Set the value attribute instead of having custom named attributes. And if you really need to use custom attributes you can, but you have to prefix them with data-. Then you can access them with jQuery's .data();

<input type="hidden" id="test" name="test" value="3" data-key="44" />

Handle via jQuery:

var key = $('#test').data('key') // Get value of data-key
var key = $('#test').attr('data-key') // DON'T DO THIS. Will work, but use data()
$('#test').data('key', 'test') // Set value
var value = $('#test').val() // Gets the `value` attribute

All that gets sent to PHP are the name and value attributes:

$val = $_GET['test'] // refers to `name` attribute. gets `value` attribute

Upvotes: 1

Andrei
Andrei

Reputation: 56716

You are not specifying name attribute for the inputs, therefore they are not added to the set of request parameters. Try adding your hidden parameters the following way:

var saccess=$('#selaccess').find(":selected").attr('key');
$(this).append('<input/>')
       .attr('type','hidden')
       .attr('name', 'selaccess')
       .attr('value', saccess);

Upvotes: 3

Related Questions