1

I'm currently adapting a shipping plugin that has a form for people to submit their tracking number and shipping company.

The template checks the variable, and if it exists, it generates the proper information for the tracking number and company. If the variable doesn't exist, it shows the form.

basically this

if($ttr<>"") {
      All the business logic
} else {
    <form method="get">
        <input type="text" name="tracknum" />
        <select name="company" class="smallForm" id="company">
                (...options...)
        </select>
        <input type="submit"/>
        <input type="hidden" name="orderAction" value="track"/>
    </form>
}

The original check for this this variable looks like this

if (isset($_POST['tracknum'])) 
$ttr = $_POST['tracknum']; 
else $ttr = $_GET['tracknum'];

So for I put each check in its own variable function, as so

public function ttr()
{
   if (isset($_POST['tracknum'])) 
       $ttr = $_POST['tracknum']; 
   else $ttr = $_GET['tracknum'];
   return $ttr;
}

and call it thusly

{% if craft.PluginName.ttr|length %}
    All the business Logic
{% else %}
    <form method="get">
        <input type="text" name="tracknum" />
        <select name="company" class="smallForm" id="company">
                (...options...)
        </select>
        <input type="submit"/>
        <input type="hidden" name="orderAction" value="track"/>
    </form>
{% endif %}

This currently returns

Undefined index: tracknum

Specifically on the line

else $ttr = $_GET['tracknum'];

Now this makes sense to me, as the form hasn't been submitted, so There should be nothing in the $_POST or $_GET. But if that's the case, why does it work on the original website, but not here?

Thanks!

1

You could do something like this to guard against it:

public function ttr()
{
    $ttr = null;

    if (isset($_POST['tracknum'])) 
        $ttr = $_POST['tracknum']; 
    else if (isset($_GET['tracknum'])
        $ttr = $_GET['tracknum'];

    return $ttr;
}
| improve this answer | |
2

"... why does it work on the original website, but not here?"

Undefined index is a notice level PHP error. Which means it's barely important, and can generally be ignored.

This is probably a result of either:

  • Your PHP error level settings are more strict on this machine, and/or
  • Craft is in devMode on this machine.
| improve this answer | |
  • Thanks, it is true, it was only showing up on my development localhost. Good to know! – P. Stucker Jul 10 '17 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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