0

I am trying to pass 2 strings from first.php file to second.php file-

In my first.php file, I tried to send data by

<?php echo "<a href='second.php?newFormat=$statusString&MACAddress=$macaddress'>link</a>"; ?>

and

<a href="second.php?newFormat="<?php echo $statusString; ?> &MACAddress=<?php echo $macaddress; ?>link</a>

and

<a href="second.php?newFormat=<?php echo urlencode($statusString);?>&amp;MACAddress=<?php echo urlencode($macaddres); ?>"link</a>

I tried all of above possibilities. I get the same error -

PHP Parse error: syntax error, unexpected '<' in /var/www/html/first.php on line 176

My $statusString is - "u=500000;t1=1479394;s=10;r=-33;v=3.7;"

My $macaddres is - "500000"

Can anyone tell me how differently do I do this?

EDIT 1

I have tried to comment this line in my code, and the code works without any errors!

EDIT 2

if(Some condition)
{   
     $statusString = "u=".$macaddress.";t1=".$time.";s=10;r=-33;v=".$voltage.";";
    <?php echo "<a href='second.php?newFormat=$statusString&MACAddress=$macaddress'>link</a>"; ?>
}

In my second.php file, I am retrieving these by-

if (isset($_GET['newFormat']))
{ 
    $str = $_GET['newFormat'];
}
else 
    $str = 'no data';
 if (isset($_GET['MACAddress']))
 { 
    $macAddress = $_GET['MACAddress'];
 }
SMG
  • 95
  • 1
  • 2
  • 11
  • No error in the first one. You have other code before that is causing it. Maybe you have already opened the php tag somewhere else? – AbraCadaver Nov 17 '16 at 16:30
  • @AbraCadaver I have commented this line and the rest of the code works well without it! – SMG Nov 17 '16 at 16:31
  • 1
    The latter two attempts are kind of weird, one has a quoting error in the HTML and the other is trying to use an HTML-encoded `&` in the URL. But there's nothing wrong with the first attempt. Perhaps the context surrounding this line is the problem? Are you sure that's line 176? What happens if you replace it with `` ? – David Nov 17 '16 at 16:32
  • 1
    Get rid of the `` on the first one. – AbraCadaver Nov 17 '16 at 16:33
  • 2
    Can we have line 176 (hopefully with a couple of lines before and after). A pastebin would be great. – Qrchack Nov 17 '16 at 16:34
  • @David It's the same error. Commenting - ``, the code works fine – SMG Nov 17 '16 at 16:36
  • 1st example is fine. 2nd example has an arbitrary quote `newFormat="` and you're missing the closing `>` tag. 3rd example also missing closing `>` tag and you mis-spelled `$macaddress` – mister martin Nov 17 '16 at 16:37
  • 1
    @SMG: That first line of code is demonstrably working. You can test it by itself at http://phpfiddle.org/ Unless you can provide code which actually demonstrates the problem, I doubt there's much anybody here can do. – David Nov 17 '16 at 16:39
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – mister martin Nov 17 '16 at 16:42

2 Answers2

1

In your last edit:

if(Some condition)
{   
    $statusString = "u=".$macaddress.";t1=".$time.";s=10;r=-33;v=".$voltage.";";
    <?php echo "<a href='second.php?newFormat=$statusString&MACAddress=$macaddress'>link</a>"; ?>
}

Well, that explains the unexpected <. You're already in the context of PHP code. So you can just write PHP code:

if(Some condition)
{   
    $statusString = "u=".$macaddress.";t1=".$time.";s=10;r=-33;v=".$voltage.";";
    echo "<a href='second.php?newFormat=$statusString&MACAddress=$macaddress'>link</a>";
}

No need to sprinkle in more <?php ?> tags, you're just confusing the PHP parser.


Edit: You should probably also URL-encode your values, since they look somewhat complex:

echo '<a href="second.php?newFormat=' . urlencode($statusString) . '&MACAddress=' . urlencode($macaddress) . '">link</a>';
David
  • 208,112
  • 36
  • 198
  • 279
  • `link` Now I get on this on the console. But my second.php isnt able to pick these up. – SMG Nov 17 '16 at 16:51
  • @SMG: Since the values have a variety of characters in them which may confuse a URL format, you should probably URL-encode them. I've updated the answer with an example. – David Nov 17 '16 at 16:55
  • @SMG: You run this script *from the command line*? All this particular code does is `echo` output. What functionality do you expect to *invoke* `second.php`? I guess it's not clear to me why you *think* `second.php` should be invoked if you're not actually clicking on that link on a web page. – David Nov 17 '16 at 17:08
0

I suppose this

<?php echo "<a href='second.php?newFormat=$statusString&MACAddress=$macaddress'>link</a>"; ?>

isn't the first line in your file. So you should delete the <?php and the ?>, because since it's a PHP file, there should already be a php start tag.

GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40