Jump to content

Recommended Posts

Phoenix 1.0.1.x

As I stated in my past post (Where to load jQuery), I'm trying to track down a problem. In doing so, I've found a couple of errors and warning. This one says:

Quote

SyntaxError: expected expression, got '<'

This comes from a modal login module that @auzStar wrote. Here is the code segment in question:

Quote

  var login = "<?php echo tep_href_link('login.php', '', 'SSL') ?>";
  if ($.contains(document, $("a[href='"+login+"']")[0]) && "<?php echo basename($PHP_SELF) != 'login.php' ?>") {
    $("a[href='"+login+"']").click(function(e) {
      e.preventDefault();
      set_snapshot("<?php echo (basename($PHP_SELF) == 'logoff.php' || basename($PHP_SELF) == 'create_account.php' || basename($PHP_SELF) == 'account_pwa.php' ? 'index.php' : basename($PHP_SELF)) ?>");
      show_login_modal();
    });
  }

The specific character causing the error is the '<' here: var login = "<?php echo ...

The script works, but I would like to fix any errors and warnings. Any ideas?

TIA

Malcolm


Get the latest Responsive osCommerce CE (community edition) here .

Share this post


Link to post
Share on other sites

The problem is that the whole lot is being output as a string to the file instead of getting processed by php to substitute the right things into the script.

It needs more context than just the bit you've quoted but it looks like a mixture of quote marks which won't help


For a new install or if your store isn't mobile-friendly, get the community-supported responsive osCommerce (Phoenix).

here: https://github.com/gburton/Responsive-osCommerce/releases

Working on generalising bespoke solutions for Quickbooks integration, Easify integration and pay4later (DEKO) integration at 2.3.x

Share this post


Link to post
Share on other sites

@BrockleyJohn

I know nothing about javascript, but I suspect you are right. It didn't look right to me, mixing PHP in there like that. But, the script works. Unless someone comes up with a solution, I may just have to live with it.

Thank you!

M


Get the latest Responsive osCommerce CE (community edition) here .

Share this post


Link to post
Share on other sites

Shouldn't every php statement be ended with a semicolon before each "?>"  ?

Share this post


Link to post
Share on other sites

@kgtee Like I said, I know nothing about javascript. The syntax error is complaining about the first <

M


Get the latest Responsive osCommerce CE (community edition) here .

Share this post


Link to post
Share on other sites
Posted (edited)

Just saw a statement somewhere that does away with double quotes:

Quote

var my_var = <?php echo json_encode($my_var); ?>;

Will this work?

Edited by kgtee

Share this post


Link to post
Share on other sites
10 hours ago, ArtcoInc said:

@kgtee Like I said, I know nothing about javascript. The syntax error is complaining about the first <

M

You don't need to; it's a php error 🙃

The end result is a script error but it's caused by a bug in the php. You just need to get the php to output the script with the echos being processed. If you post the whole function (or template file) we can point you in the right direction.


For a new install or if your store isn't mobile-friendly, get the community-supported responsive osCommerce (Phoenix).

here: https://github.com/gburton/Responsive-osCommerce/releases

Working on generalising bespoke solutions for Quickbooks integration, Easify integration and pay4later (DEKO) integration at 2.3.x

Share this post


Link to post
Share on other sites
12 hours ago, ArtcoInc said:

@BrockleyJohn

But, the script works.

If it's throwing an error then it doesn't work... and it could be stopping other stuff working too, depending on the context of the error. Maybe the addon appears to function but it's a good bet that it would do more or do it differently with the bug(s) fixed.


For a new install or if your store isn't mobile-friendly, get the community-supported responsive osCommerce (Phoenix).

here: https://github.com/gburton/Responsive-osCommerce/releases

Working on generalising bespoke solutions for Quickbooks integration, Easify integration and pay4later (DEKO) integration at 2.3.x

Share this post


Link to post
Share on other sites

Just tested the script and it works fine without giving any errors? Modal works fine ( I stripped out all the Ajax stuff) so it just pain old boostrap modal. Pops up every time page is refreshed unless you login , logs in fine beyond no idea as I don't use it.

image.thumb.png.7368c7e722d38a1077709e142bed3e5a.png


 

Share this post


Link to post
Share on other sites

@JcMagpie

Back in 2015, I started a thread about using a Modal to log in faster ...

In this thread, I came up with a simple modal popup to make logging in faster, without having to load another page. There was a lot of discussion on how the login was handled in a mixed SSL site.

@auzStar added the scripts to "set a snapshot". It is in these scripts that this error is happening.

Later, @ndiggity added the Ajax functionality. I have not used this. I am using @auzStar's version.

@BrockleyJohn here is the full script. Initially, this was all in a navbar module template file. I had to pull the scripts out and move them to a header tag module in order to load the code at the bottom of the page.


 

<?php
/*
  $Id: ht_modal_login.php, v1.0 27th Aug. 2019 M.Spann$
  Modal Login script Header Tag Module

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2016 osCommerce

  Released under the GNU General Public License
*/

  class ht_modal_login {
    var $code;
    var $group;
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;

    function __construct() {
      $this->code = get_class($this);
      $this->group = 'footer_scripts';

      $this->title = MODULE_HEADER_TAGS_MODAL_LOGIN_TITLE;
      $this->description = MODULE_HEADER_TAGS_MODAL_LOGIN_DESCRIPTION;

      if ( defined('MODULE_HEADER_TAGS_MODAL_LOGIN_STATUS') ) {
        $this->sort_order = MODULE_HEADER_TAGS_MODAL_LOGIN_SORT_ORDER;
        $this->enabled = (MODULE_HEADER_TAGS_MODAL_LOGIN_STATUS == 'True');
      }
    }

    function execute() {
      global $PHP_SELF, $oscTemplate;

      $output = <<<EOD

<script>
  $(function() {

// login (login is also in footer, index page and create account page)
    var login = "<?php echo tep_href_link('login.php', '', 'SSL') ?>";
    if ($.contains(document, $("a[href='"+login+"']")[0]) && "<?php echo basename($PHP_SELF) != 'login.php' ?>") {
      $("a[href='"+login+"']").click(function(e) {
        e.preventDefault();
        set_snapshot("<?php echo (basename($PHP_SELF) == 'logoff.php' || basename($PHP_SELF) == 'create_account.php' || basename($PHP_SELF) == 'account_pwa.php' ? 'index.php' : basename($PHP_SELF)) ?>");
        show_login_modal();
      });
    }


// if login modal is closed then we clear the snapshot
// (if continues to login, then snapshot is cleared as normal via login.php after successful login)
    $('#ModalLogin').on('hidden.bs.modal', function() {
      $.ajax({ url: "ext/modules/content/header/login_modal/clear_snapshot.php" });
    })

  });


  function set_snapshot(redirect_page, redirect_vars, redirect_ssl) {
    var redirect_vars = redirect_vars || {};
    redirect_vars = $.extend(<?php echo json_encode($_GET) ?>, redirect_vars);
    var redirect_ssl = redirect_ssl || '';
    $.ajax({ type: "GET", dataType: "json", url: "ext/modules/content/header/login_modal/set_snapshot.php", data:   {'redirect_ssl': redirect_ssl, 'redirect_page': redirect_page, 'redirect_vars': redirect_vars} });
  }


  function show_login_modal() {
    $('#ModalLogin').modal('show');
  }

</script>
EOD;


      $oscTemplate->addBlock($output, $this->group);
    }


    function isEnabled() {
      return $this->enabled;
    }

    function check() {
      return defined('MODULE_HEADER_TAGS_MODAL_LOGIN_STATUS');
    }

    function install() {
      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable New Equal Heights Module', 'MODULE_HEADER_TAGS_MODAL_LOGIN_STATUS', 'True', 'Do you want to enable the New Equal Heights module?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");

      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_HEADER_TAGS_MODAL_LOGIN_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '3', now())");
    }

    function remove() {
      tep_db_query("delete from configuration where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_HEADER_TAGS_MODAL_LOGIN_STATUS',
                   'MODULE_HEADER_TAGS_MODAL_LOGIN_SORT_ORDER');
    }

  }

TIA

Malcolm


Get the latest Responsive osCommerce CE (community edition) here .

Share this post


Link to post
Share on other sites
Posted (edited)
1 hour ago, ArtcoInc said:

Later, @ndiggity added the Ajax functionality

This is the one I downloaded. It works with or without the Ajax files, All I have done is make it a footer add-on and move the modal call script to a sitewide hook and add aditional js to stop it showing evertime a page is refreshed. So far all working as expected Ajax works fine not getting any script sintax errors logged at all.

Edited by JcMagpie
update error

 

Share this post


Link to post
Share on other sites

Js fiddle is complaing about this line in your script adding "" makes js fiddle happy.

redirect_vars = $.extend("<?php echo json_encode($_GET) ?>", redirect_vars);

 


 

Share this post


Link to post
Share on other sites

The problem here is the way that what I guess was a template file in a content module has been changed into inline code in an execute function in a header tags module.

It's using a heredoc string definition (the bit with the <<<) but still has the previous html/javascript with <?php echo...;?> bits. To get these to work in this context you would have to set them up in variables up front and then substitute them in the string. If that's all a bit complicated then an alternative is the grunt work of changing the code to lots of bits of quoted (and appropriately escaped) bits of string and concatenating them.

By far the easiest approach to a conversion like this without much programming is to keep a separate template file. Header tags modules don't usually have a template file but that doesn't mean you can't have one!


For a new install or if your store isn't mobile-friendly, get the community-supported responsive osCommerce (Phoenix).

here: https://github.com/gburton/Responsive-osCommerce/releases

Working on generalising bespoke solutions for Quickbooks integration, Easify integration and pay4later (DEKO) integration at 2.3.x

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×