WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form DB
[resolved] Compare result of custom shortcode output with passed value (3 posts)

  1. Bob
    Member
    Posted 1 year ago #

    I have a CF7 form, the CFDB plugin and 'shortcode exec PHP'

    My form passes a value in the URL using GET to a page from which I call a custom shortcode which I want to use to compare the value passed in the URL to the values in a specific field of a CFDB table.

    If the value isn't found, redirect to another page, passing a value (mydate) in the url. If it is found, output a message and stop there.

    Heres the shortcode code:

    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $clientdate = $_GET['mydate'];
    $exp->export($atts['Bookings'], array('show' => 'booked', 'search' => $clientdate));
    
       if ($result == "")
       {
          header('Location: http://mysite.com/our-availability?mydate='.$clientdate);
       }
       else
       {
          echo '<p>We are sorry - this date is already booked.<br />Would you like to <a href="http://www.mysite.com/availability">check another date?</a></p>';
       }

    I just need to figure out how to make the variable named $result hold the value that is output by $exp

    Can anyone help please?

    http://wordpress.org/extend/plugins/contact-form-7-to-database-extension/

  2. Bob
    Member
    Posted 1 year ago #

    I experimented with the built-in shortcodes and in the page that receives the date passed in the URL from the form I entered:

    [cfdb-value form="Bookings" show="booked" filter="booked=$_GET(mydate)"]

    This displays a date string if the date entered on the form matches one in my DB. If there's no matching date in the DB it displays an empty string (i.e. nothing).

    I copied and amended the PHP behind this built-in shortcode so my custom shortcode now looks like this:

    if (isset($atts['form'])) {
                $atts['fromshortcode'] = true;
                require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/ExportToValue.php');
                $export = new ExportToValue();
                return $export->export($atts['form'], $atts);
                if(!empty($export))
                {
                echo "Your date is available";
                }
                else
                {
                echo "Your date is unavailable";
                }
            }

    This does exactly what the built in shortcode does but I hoped it would test if $export was empty. It doesn't seem to. Isn't $export a variable containing a string? If not how do I create a variable and put the date that the shortcode is returning into it to test it?

  3. Bob
    Member
    Posted 1 year ago #

    FIXED IT!

    For anyone who's interested, ultimately what I was looking to do was to check a value that had been passed to my page URL using GET from a ContactForm 7 Form, and use a shortcode to compare it to a DB table. If there's a match, do one thing otherwise do another.

    Here's the custom shortcode as it appears on the WordPress page:

    [yourcustomshortcodename form="your-formname" show="yourfieldname" filter="yourfieldname=$_GET(yourvaluefromurl)"]

    Here's the custom shortcode PHP entered using Shortcode Exec PHP plugin:

    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $exp->export($atts['form'], $atts);
    if ($row = $exp->nextRow()) {
          echo '<p>Your value is in the database</p>';
          }
    else  {
          echo '<p>Your value is not in the database</p>';
          }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.