0

I set up a shortcode which includes a standard php/html form file. In the form I have a hidden field where the value is a variable which is meant to be available in the form via the shortcode attributes.

The problem is that the value for clientId is not being passed from:

[insert-form form_location="form.php" clientId="25052"]

Instead I am getting the value defined as the default (NULL). See the code below.

Shortcode:

function insert_the_form($atts){
$form_base = plugin_dir_path(__DIR__);

// Shortcode attributes & options
$atts = shortcode_atts(
    array(
        'form_location' => 'NULL',
        'clientId' => 'NULL' //Variable for client ID
        ), $atts, 'cm_insert_cmform'
    );

if ( isset($atts['form_location']) ){
    $form_location = $atts['form_location'];
}
if ( isset($atts['clientId']) ){
    $clientId = $atts['clientId'];
}   

ob_start();
include($form_base . 'forms/' . $form_location);
$out = ob_get_clean();

return $out;
}

add_shortcode( 'insert-form', 'insert_the_form' );

The form:

<form id="aform" method="post" action="/">
<input type="hidden" name="clientid" id="clientid" value="<?php echo $clientId; ?>" />
<input type="submit" id="submitButton" value="Submit" class="button" />
</form>
2
  • 2
    Files that are being included do not have the original file's variables in scope in php. This SO answer has a good explanation. Edit: I might make a class of static methods where each method echoed the markup of the form and accepted the $clientId as a parameter. Nov 26, 2014 at 19:32
  • Ok, that helps clarify what I'm doing wrong. Thanks.
    – greg
    Nov 27, 2014 at 12:34

2 Answers 2

0

Ok, so somehow I got it working by changing it to this: It looks like it didn't like 'the_clientId'. Maybe it just doesn't like capital letters????

[insert-form form_location="form.php" identification_number="12345"]


function insert_the_form($atts){
  $form_base = plugin_dir_path(__DIR__);

  // Shortcode attributes & options
  $atts = shortcode_atts(
  array(
    'form_location' => 'NULL',
    'identification_number' => 'NULL' //Variable for client ID
    ), $atts, 'insert_the_form'
  );

if ( isset($atts['form_location']) ){
  $form_location = $atts['form_location'];
}
if ( isset($atts['identification_number']) ){
  $form_idnumber = $atts['identification_number'];
}   

$clientId = $form_idnumber;

ob_start();
include($form_base . 'forms/' . $form_location);
$out = ob_get_clean();

return $out;
}

add_shortcode( 'insert-form', 'insert_the_form' );
-2

Using SESSION, storing $clientId to SESSION and calling it back in FORM

1
  • 1
    This should be a comment. Please don't use the answer section to post comments. You will be able to comment once have earned enough reputation Nov 27, 2014 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.