Jump to content

Archived

This topic is now archived and is closed to further replies.

anner

Posting variable containing \n

Recommended Posts

I'm trying to post a variable that contains data like this:
[code]
first.domain.tld\nsecond.domain.tld\nthird.domain.tld
[/code]
I know that's what's getting posted (I have it working in a perl script elsewhere), but when I do
[code]
$Domains = $_POST['Domains'];
[/code]
I only get:
third.domain.tld

any idea why?

Share this post


Link to post
Share on other sites
Please post the code you're currently using.

Ken

Share this post


Link to post
Share on other sites
\n simply makes a new line in txt code
Mainly used to keep your source code clean, this should not effect the var
As Ken said, Lets see your code

Share this post


Link to post
Share on other sites
The code is long.

Here is the perl code (called in an include) that does the posting:
[code]sub View_Admin
{
  # Get the admin name from the passed parameter
  my $AdminID = $_[0];

  # Define some variables
  my $DataFile = $Directory . "/" . $AdminID . ".dat";
  my $line = "";      # A line of data from the admin file
  my @List = "";      # A Container for Domain/Rights to be passed
  my @Groups = Get_Groups();  #get full grouplist for SuperAdmin
  my $Group = "";
  my @Domains = "";
  my @Rights = "";

  ################################################################################
  # Start printing html with a nice header                                      #
  ################################################################################
  # Print the main html header
  $Title =  "Modify Administrator Data for $AdminID";
  Print_Title($Title);
  print "<head>";
  # Print some Java script to verify delete choice
  print "<script language='JavaScript'>
function Confirm()
{
  return confirm('Are you sure you want to delete this NCMail Administrator?');
}
</script>\n";
# Java script
  print "<SCRIPT LANGUAGE=\"JavaScript\"><!--\n";
  print "function deleteOption(object,index) {\n";
  print "    object.options[index] = null;\n";
  print "}\n";

  print "function addOption(object,text,value) {\n";
  print "    var defaultSelected = true;\n";
  print "    var selected = true;\n";
  print "    var optionName = new Option(text, value, defaultSelected, selected)\n";
  print "    object.options[object.length] = optionName;\n";
  print "}\n";

  print "function copySelected(fromObject,toObject) {\n";
  print "    for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
  print "        if (fromObject.options[i].selected)\n";
  print "        addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);\n";
  print "    }\n";
  print "    for (var i=fromObject.options.length-1;i>-1;i--) {\n";
  print "        if (fromObject.options[i].selected)\n";
  print "            deleteOption(fromObject,i);\n";
  print "    }\n";
  print "}\n";

  print "function copyAll(fromObject,toObject) {\n";
  print "    for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
  print "        addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);\n";
  print "    }\n";
  print "    for (var i=fromObject.options.length-1;i>-1;i--) {\n";
  print "        deleteOption(fromObject,i);\n";
  print "    }\n";
  print "}\n";

  print "function selectAll(fromObject) {\n";
  print "    for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
  print "        fromObject.options[i].selected = true;\n";
  print "    }\n";
  print "}\n";

  print "//--></SCRIPT>\n";

  print "</head>\n";

  # if the user is a regular admin, we want a little different message here.
  if ($SuperAdmin)
  {
      print "Any data in a text entry box can be changed, including the password.<br>\n";
  }
  else
  {
      print "Data in a text entry box can be changed, including the password. Contact the ITS Customer Service Center at (919) 981-5197 or (800)
722-3946 to change the other data.\n";

  }

  print "<form action=http://my.admin.server/adminmodify.cgi method=POST>
      <hr size=5>
      <input type=hidden name=AdminID value=$AdminID>
      <input type=hidden name=Datafile value=$DataFile>";

  # open the data file for the admin selected
  $temp = open (ADMIN, "<$DataFile");
  if (!$temp)
  {
      $error_message = "Unable to open file $DataFile for Read/Write.";
      Log_Error ( $error_message, $html_flag );
      Quit();
  }
  my @Data = (<ADMIN>);
  # close the file
  close ADMIN;

  for ($i=0;$i<=$#Data;$i++)
  {
      # get the line of data
      $line = $Data[$i];
      chomp($line);  # remove any trailing new line

      if ($i == 0)
      {
        # prints description of line and the data
        print "Password: <input type=text size=20 value='$line' name=Password><br>\n";
      }
      elsif ($i == 1)
      {  #If user is not SuperAdmin, don't allow the Name to be changed.
        if ($SuperAdmin)
        {
        # Put quotes around the name so ones with spaces work correctly in form
        $line = "\"$line\"";
        # prints description of line and the data
        print "Admin Name: <input type=text size=30 value=$line name=Admin><br>\n";
        } else
        {
        # prints description of line and the data
        print "Admin Name: $line <br>\n";
        # Put quotes around the name so ones with spaces work correctly in form
        $line = "\"$line\"";
        print "<input type=hidden value=$line name=Admin>\n";
        }
      }
      elsif ($i == 2)
      {  #If user is not SuperAdmin, don't allow the Email to be changed
        if ($SuperAdmin)
        {
        # prints description of line and the data
        print "E-Mail Address: <input type=text size=30 value='$line' name=Email><br>\n";
        }else
        {
        # prints description of line and the data
        print "E-Mail Address: $line<br> <input type=hidden value='$line' name=Email>\n";
        }
      }
      elsif ($i == 3)
      {
        # prints description of line and the data
        print "Phone Number: <input type=text size=30 value='$line' name=Phone><br>\n";
      }
      elsif (($line =~ /\<domain/) and not ($line =~ /\/domain/))
      {
        # start of domain data
      }
      elsif (($line =~ /\<rights/) and not ($line =~ /\/rights/))
      {
        # start of rights data

      }
      else
      {
              # if the end of the domain list so put the collected data into a variable to pass, then reset
            if ($line =~ /\/domain/)
            {
              @Domains = @List;
              @List = "";
            }
            else {
              # if the end of the rights list so put the collected data into a variable to pass, then reset
              if ($line =~ /\/rights/)
              {
              @Rights = @List;
              @List = "";
              }
              else {
              # This prints all the regular lines which by default must be domains or rights
              # put them in array for submission
              push (@List, "$line\n" );
              }
          }
        }

      }

  $seen{$_} = 1 for @Domains;
  @Groups = grep ! $seen{$_}, @Groups;

  print "<h3>Domains</h3>";

  if ($SuperAdmin)
        {
            print "<table cellpadding=3 border=2>\n";
            print "<tr><th $trshade>Available Groups</th><th $trshade>&nbsp;</th><th $trshade>Current Groups</th></tr>\n";
            # drop-down of available groups
            print "<tr><td align=center><select size=10 multiple name=Allgrps>\n";
            foreach $Group (@Groups)
            {
              print "<option>$Group</option>\n";
            }

            print "</select></td>";
            # dynamic add/remove buttons
            print "<td valign=center><input type=button name=grp-add value='--- Add ---&gt;&gt;'";
            print " onClick=\"if (document.images) copySelected(this.form.Allgrps,this.form.Domains)\">";
            print "<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>";
            print "<input type=button name=grp-remove value='&lt;&lt;- Remove'";
            print " onClick=\"if (document.images) copySelected(this.form.Domains,this.form.Allgrps)\">";
            print "</td>";
            # Text box list of all groups for contact
            print "<td align=center><select size=10 multiple name=Domains>";     
            foreach $line(@Domains)
            {
            if ($line) {print "<option>$line\n</option>";}
            }
            print "</select></td></tr>\n";
            # end table
            print "</table>\n";
        }
        else
        {
            print "<input type=hidden value='@Domains' name=Domains>\n";
            foreach $line(@Domains)
            {
            if ($line) {print "$line<br>\n";}
            }
        }

  print "<br><h3>Right Limitations (if any)</h3>";

  if ($SuperAdmin)
  {
    print "<textarea name=Rights cols=50 rows=5>";
    foreach $line(@Rights)
    {
      if ($line) {print "$line\n";}
      }
    print "</textarea>";

  }
  else
  {
    print "<input type=hidden value='@Rights' name=Rights>";
    foreach $line(@Rights)
    {
      if ($line) {print "$line\n";}
      }
  }

  print "<hr>";
  print "<input type=submit name=Modify value='Submit Changes' ";
  if ($SuperAdmin)
  {
    print "onClick=\"if (document.images) selectAll(this.form.Domains)\">\n";
  }
  else
  {
    print ">&nbsp;&nbsp;&nbsp;\n";
  }
  print end_html;

  # If we are a SuperAdmin print a button to delete the admin
  if ($SuperAdmin)
  {
      print "<input type=submit name=Delete value='Delete This Admin' onClick='return Confirm()'><p>\n";
  }
}

[/code]

And, as I said, this works fine perl to perl, but when the perl code is included, I only get the last domain:
[code]
<?php
global $user;
$function = 'http://my.admin.server/AdminFunctions/adminmodify.cgi?User=';
$send = $function . $user->name;

if ($_POST['Search'])
{$Search = $_POST['Search'];
$send = $send . "&Search=" . urlencode($Search);
}
elseif ($_POST['Show'])
{$Show = $_POST['Show'];
$send = $send . "&Show=" . urlencode($Show);
}
elseif ($_POST['Modify'])
{$Modify = $_POST['Modify'];
$send = $send . "&Modify=" . urlencode($Modify);
}
elseif ($_POST['Delete'])
{$Delete = $_POST['Delete'];
$send = $send . "&Delete=" . urlencode($Delete);
}
else
{$Main = "Main";
$send = $send . "&Main=" . urlencode($Main);
}
if ($_POST['AdminID'])
{$AdminID = $_POST['AdminID'];
$send = $send . "&AdminID=" . urlencode($AdminID);
}
if ($_POST['Password'])
{$Password = $_POST['Password'];
$send = $send . "&Password=" . urlencode($Password);
}
if ($_POST['Admin'])
{$Admin = $_POST['Admin'];
$send = $send . "&Admin=" . urlencode($Admin);
}
if ($_POST['Email'])
{$Email = $_POST['Email'];
$send = $send . "&Email=" . urlencode($Email);
}
if ($_POST['Phone'])
{$Phone = $_POST['Phone'];
$send = $send . "&Phone=" . urlencode($Phone);
}
if ($_POST['Domain'])
{$Domain = $_POST['Domain'];
$send = $send . "&Domain=" . urlencode($Domain);
}
if ($_POST['Domains'])
{$Domains = $_POST['Domains'];
echo $_POST['Domains'];
$send = $send . "&Domains=" . urlencode($Domains);
}
if ($_POST['Rights'])
{$Rights = $_POST['Rights'];
$send = $send . "&Rights=" . urlencode($Rights);
}
if ($_POST['Datafile'])
{$Datafile = $_POST['Datafile'];
$send = $send . "&Datafile=" . urlencode($Datafile);
}

echo $send;
include $send;

?>
[/code]

Share this post


Link to post
Share on other sites
Also, If I have my php echo Domains:
[code]
echo $_POST['Domains'];
[/code]
I only see that last domain from the list.

Share this post


Link to post
Share on other sites
Where is the code for the form that generates the $_POST['Domains']?

Ken

Share this post


Link to post
Share on other sites
It's in the first code box in my post above, as indicated.

This is what it ends up looking like in the source code:
[code]
<html><head>
          <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
          <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
          <META HTTP-EQUIV="Expires" CONTENT="-1">
          </head>
          <body bgcolor=#aaaaaa>
<table align=center width=100%>
          <tr>
          <td></td>
          <td><h1 align=center><font size=5>Modify Administrator Data for bogus.admin</font></h1></td>
          <td></td>

          </tr></table><hr size=5>
<head><script language='JavaScript'>
function Confirm()
{
  return confirm('Are you sure you want to delete this Administrator?');
}
</script>
<SCRIPT LANGUAGE="JavaScript"><!--
function deleteOption(object,index) {
    object.options[index] = null;
}
function addOption(object,text,value) {
    var defaultSelected = true;
    var selected = true;
    var optionName = new Option(text, value, defaultSelected, selected)
    object.options[object.length] = optionName;
}
function copySelected(fromObject,toObject) {
    for (var i=0, l=fromObject.options.length;i<l;i++) {
        if (fromObject.options[i].selected)
        addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
    }
    for (var i=fromObject.options.length-1;i>-1;i--) {
        if (fromObject.options[i].selected)
            deleteOption(fromObject,i);
    }
}
function copyAll(fromObject,toObject) {
    for (var i=0, l=fromObject.options.length;i<l;i++) {
        addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
    }
    for (var i=fromObject.options.length-1;i>-1;i--) {
        deleteOption(fromObject,i);
    }
}
function selectAll(fromObject) {
    for (var i=0, l=fromObject.options.length;i<l;i++) {
        fromObject.options[i].selected = true;
    }
}
//--></SCRIPT>
</head>
Any data in a text entry box can be changed, including the password.<br>
<form action=http://my.admin.server/AdminModify.cgi method=POST>
      <hr size=5>
      <input type=hidden name=User value=anne.ramey>
      <input type=hidden name=AdminID value=bogus.admin>
      <input type=hidden name=Datafile value=/adminfile/bogus.admin.dat>Password: <input type=text size=20 value='XXXXXX' name=Password><br>

Admin Name: <input type=text size=30 value="Bogus Administrator" name=Admin><br>
E-Mail Address: <input type=text size=30 value='test.admin@mydomain.com' name=Email><br>
Phone Number: <input type=text size=30 value='919-431-1590' name=Phone><br>
<h3>Domains</h3><table cellpadding=3 border=2>
<tr><th >Available Groups</th><th >&nbsp;</th><th >Current Groups</th></tr>
<tr><td align=center><select size=10 multiple name=Allgrps>
<option>admin.ah.net.grp</option>
<option>admin.net.grp</option>
<option>com.grp</option>
</select></td><td valign=center><input type=button name=grp-add value='--- Add ---&gt;&gt;' onClick="if (document.images) copySelected(this.form.Allgrps,this.form.Domains)"><br>&nbsp;<br>&nbsp;<br>&nbsp;<br><input type=button name=grp-remove value='&lt;&lt;- Remove' onClick="if (document.images) copySelected(this.form.Domains,this.form.Allgrps)"></td><td align=center><select size=10 multiple name=Domains><option>test.domain.one


</option><option>test.this.net

</option></select></td></tr>
</table>
<br><h3>Right Limitations (if any)</h3><textarea name=Rights cols=50 rows=5></textarea><hr><input type=submit name=Modify value='Submit Changes' onClick="if (document.images) selectAll(this.form.Domains)">

</body>
</html>[/code]

Just to be really clear:
The first snippet of code in the above post is the perl script that is included and actually does the posting.  I know this part is working correctly because it works fine when used by itself.  It only fails when included in the php script (the second snippet of code above).

Share this post


Link to post
Share on other sites
I believe the problem here is that you're "including" a Perl script into a PHP script. Maybe you really want to transfer to that script with the header() function.

So change:
[code]<?php include $send; ?>[/code]
to
[code]<?php header('Location: ' . $send); ?>[/code]
For this to work, you will have to remove all of your "echo" statements.

Ken

Share this post


Link to post
Share on other sites
Nah, I'm pretty sure I don't want to do that.  This is inside a CMS...there are headers, etc already.

I've tried \t and , as well as \n and get the same result, so it's definitely not that...I'm think it's because the variable is an array.  How can I grab a posted array correctly?  I tried:
[code]
if ($_POST['Domains'])
{$Domains = array($_POST['Domains']);
echo $Domains;
$send = $send . "&Domains=" . urlencode($Domains);
}
[/code]

But this had 2 problems.  First, it didn't print out the whole array anyway, so it didn't seem to make a difference.  Second, I can't pass an array in a URL.  I can work around the second if I can just get the first figured out.

Share this post


Link to post
Share on other sites
I don't see why the \n would force new elements into yoru array instead of just appending to the string, but I'm not going to question your error.  That's a bit pointless.

This is a bit of a longshot, but my guess is that it's a multidimensional array.  Try putting a second key on there and seeing if that yields any different results.

$_POST['Domains'][0];
$_POST['Domains'][1];
etc...

Share this post


Link to post
Share on other sites
At the start of your PHP script put in the following line and post the result:
[code]<?php if (isset($_POST)) echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>[/code]
This will dump whatever is being posted to your script from the form.

Is there a live URL where this problem can be seen?

Ken

Share this post


Link to post
Share on other sites
I'm passing them into an array (appended with a \n) because that's how the JS textbox handles it's submission.

If I try:
[code]
if ($_POST['Domains'])
{  $Domains = "";
  foreach($_POST['Domains'] as $key => $value)
  {
    $Domains .= $value;
  }
  $send = $send . "&Domains=" . urlencode($Domains);
}
[/code]
I get:
warning: Invalid argument supplied for foreach()

If I do:
[code]
$i = 0;
$Domains = "";
while ($_POST['Domains'][$i])

    $Domains .= $_POST['Domains'][$i];
    $i++;
}
echo $Domains;
if ($Domains != "")
{
$send = $send . "&Domains=" . urlencode($Domains);
}
[/code]

I still only get the last domain.  ?

Share this post


Link to post
Share on other sites
We really can't help you anymore until we see what your script is seeing. Please post the results of the line I posted.

Ken

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=102863.msg409487#msg409487 date=1154697337]

Is there a live URL where this problem can be seen?

Ken
[/quote]

Unfortunately not.  These are secure admin functions for my organization and I'd be in deep kimshe if I let anyone not "authorize" view these pages.  I'm really appreciative of your help and will try and answer any questions about the setup.

And just to clarify another point, it's the JS select box that is creating the array, not just adding \n to the end.  That's why I tried other characters.  It appears the array is not being seen as an array.  I'll try your suggestion ken and post the results.  Thanks,

Share this post


Link to post
Share on other sites
[tt]Array
(
    [User] => anne.ramey
    [AdminID] => bogus.admin
    [Datafile] => /admin_auth/bogus.admin.dat
    [Password] => XXXXXX
    [Admin] => Bogus Administrator
    [Phone] => 919-431-1591
    [Domains] => its.net.grp
    [Rights] =>
    [Modify] => Submit Changes
)[/tt]

Here is the snippet again of the code that creates the array to be posted:
There is this JS in the head
[code]
# Java script
  print "<SCRIPT LANGUAGE=\"JavaScript\"><!--\n";
  print "function deleteOption(object,index) {\n";
  print "    object.options[index] = null;\n";
  print "}\n";

  print "function addOption(object,text,value) {\n";
  print "    var defaultSelected = true;\n";
  print "    var selected = true;\n";
  print "    var optionName = new Option(text, value, defaultSelected, selected)\n";
  print "    object.options[object.length] = optionName;\n";
  print "}\n";

  print "function copySelected(fromObject,toObject) {\n";
  print "    for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
  print "        if (fromObject.options[i].selected)\n";
  print "        addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);\n";
  print "    }\n";
  print "    for (var i=fromObject.options.length-1;i>-1;i--) {\n";
  print "        if (fromObject.options[i].selected)\n";
  print "            deleteOption(fromObject,i);\n";
  print "    }\n";
  print "}\n";

  print "function copyAll(fromObject,toObject) {\n";
  print "    for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
  print "        addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);\n";
  print "    }\n";
  print "    for (var i=fromObject.options.length-1;i>-1;i--) {\n";
  print "        deleteOption(fromObject,i);\n";
  print "    }\n";
  print "}\n";

  print "function selectAll(fromObject) {\n";
  print "    for (var i=0, l=fromObject.options.length;i<l;i++) {\n";
  print "        fromObject.options[i].selected = true;\n";
  print "    }\n";
  print "}\n";

  print "//--></SCRIPT>\n";
[/code]
And this in the body:
[code]
          print "<table cellpadding=3 border=2>\n";
            print "<tr><th $trshade>Available Groups</th><th $trshade>&nbsp;</th><th $trshade>Current Groups</th></tr>\n";
            # drop-down of available groups
            print "<tr><td align=center><select size=10 multiple name=Allgrps>\n";
            foreach $Group (@Groups)
            {
              print "<option>$Group</option>\n";
            }

            print "</select></td>";
            # dynamic add/remove buttons
            print "<td valign=center><input type=button name=grp-add value='--- Add ---&gt;&gt;'";
            print " onClick=\"if (document.images) copySelected(this.form.Allgrps,this.form.Domains)\">";
            print "<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>";
            print "<input type=button name=grp-remove value='&lt;&lt;- Remove'";
            print " onClick=\"if (document.images) copySelected(this.form.Domains,this.form.Allgrps)\">";
            print "</td>";
            # Text box list of all groups for contact
            print "<td align=center><select size=10 multiple name=Domains>";     
            foreach $line(@Domains)
            {
            if ($line) {print "<option>$line\n</option>";}
            }
            print "</select></td></tr>\n";
            # end table
            print "</table>\n";
[/code]
And is posted with this:
[code]
  print "<input type=submit name=Modify value='Submit Changes' onClick=\"if (document.images) selectAll(this.form.Domains)\">\n";
[/code]

When running this included perl script by itself, I read this variable back in with the line:
[code]
my @Groups = param('Domains'); # A single variable to contain the domains passed from the calling cgi
[/code]
And it contains all the data.

Share this post


Link to post
Share on other sites
It just doesn't appear to be recognizing that that is a multi-select box.

My text box works fine, all other posted variables work fine, it's just the fancy JS multi-select box.  arrrg

Share this post


Link to post
Share on other sites
AHHA!  Figured it out!  My multiple select box was set up like this:
[code]
<select size=10 multiple name=Domains>
[/code]
Which is apparently not good enough for php.  I changed it to:
[code]
<select size=10 multiple name=Domains[] id=Domains>
[/code]
and now I get
[tt]
Array
(
    [User] => anne.ramey
    [AdminID] => bogus.admin
    [Datafile] => /admin_auth/bogus.admin.dat
    [Password] => XXXXXX
    [Admin] => Bogus Administrator
    [Phone] => 919-431-1591
    [Domains] => Array
        (
            [0] => first.net.grp
            [1] => test.net.grp
        )

    [Rights] => ldaponly
another


    [Modify] => Submit Changes
)
[/tt]
I seem to be losing my \n, but I can work with that.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.