SMG SMG - 16 days ago 5
PHP Question

Passing 2 strings from one PHP script to the other using href

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'];
}

Answer

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>';
Comments