0

I am sending a javascript value in a hidden form to a php script. This is the code.

<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function(e) {
var mydata = 3;
if ($(this).is(':not([data-submit="true"])'))
{
        $('form').append('<input type="hidden" name="foo" value=mydata>');
        $('form').data('submit', 'true').submit();
        e.preventDefault();
        return false;
}
})
})

In my php script, I am accessing the value like below.

$src1= $_POST['foo'];
echo $src1;

I have initialized mydata to 3. I am expecting the output to be 3 in my php script, but instead I am getting the string mydata.

3
  • You've only shown us the code that works. Show us the code that doesn't work. Commented Jan 23, 2014 at 15:46
  • mydata will be consider as String not variable that you are passing. Commented Jan 23, 2014 at 15:46
  • It is very important to note that your jQuery is critically incorrect. It will break instantly if you have more than one <form> on your page. Commented Jan 23, 2014 at 15:48

4 Answers 4

3

Use this:

  $('form').append('<input type="hidden" name="foo" value="'+mydata+'">');

Here mydata is consider as variable not String.

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

Comments

2

All the problem was that you was passing mydata as string instead of as the variable itself, you need to concatenate correctly to pass the Value to the Value, as it works.

Using this Code:

<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function(e) {
var mydata = 3;
if ($(this).is(':not([data-submit="true"])'))
{
        $('form').append('<input type="hidden" name="foo" value="'+mydata+'">');
        $('form').data('submit', 'true').submit();
        e.preventDefault();
        return false;
}
})
})

You see that now, the real value into value="" html attribute of the <input> will be 3 instead of "mydata".

This way you will be able to access $_POST['foo']; on the php page, getting 3.

Comments

2

JavaScript strings do not interpolate variables. They can't - JavaScript identifiers do not start with sigils, so there is no way to distinguish a variable name in a string from a piece of text.

The quick, dirty and unsafe approach is to break apart your string and concatenate it:

$('form').append('<input type="hidden" name="foo" value="' + mydata + '">');

But this will fail if mydata contains a " and could give weird results if it contains a &, so build you HTML using DOM (or jQuery wrappers around that).

$('form').append(
    $('<input>').attr('type', 'hidden').attr('name', 'foo').attr('value', mydata)
);

Comments

2

You don't concatenate the string correctly, try this way :

$('form').append('<input type="hidden" name="foo" value="' + mydata +'">');

1 Comment

value needs to be mydata, not name

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.