Jump to content

[SOLVED] valid statement?


gevo12321

Recommended Posts

Yes, I don't see anything wrong with this code. It should work for you. However, your setting your name variable and button variable to be an array. If you want it to be $name1=$_POST[button1] you should do it like this.

 


<?php

$id=1;

$name.$id=$_POST[button.$id];

?>

 

You have to concatenate the 2 variables you want to join.

 

 

 

the code u gave me doesnt work it gives a blank page when i try to echo $name1 and it gives no errosr but actually i want $id to be an array so

 

i tryed my original code it says:

 

unexpected '[', expecting ']'

 

so i changed the code to this:

$name[$id]=$_POST[button][$id];

 

but this doesnt work either it gives me the following error:

 

Warning: Illegal offset type

 

so i guess it's not valid?

 

do you know how it might work?

 

this code is the form:

<html>
<body>
<form action="2.php" method="post">
<input type="text"  name="button1">
<input type="text"  name="button1">
<input type="submit" value="enter">
</form>
</body>
</html>

 

this code is the 2.php:

<?php
$id=array(1,2)
$name.$id=$_POST[button.$id];
echo $name1;
echo $name2;
?>

You have two options

 

<html>
<body>
<form action="2.php" method="post">
<input type="text"  name="button1">
<input type="text"  name="button2"> <!-- notice I changed this to 2, crazy how it works! -->
<input type="submit" value="enter">
</form>
</body>
</html>

 

<?php
$id=array(1,2); 
foreach ($id as $val) {
    $name[$id] = $_POST['button'.$id];
}
echo $name[1];
echo $name[2];
?>

 

Or

 

<html>
<body>
<form action="2.php" method="post">
<input type="text"  name="button[]">
<input type="text"  name="button[]">
<input type="submit" value="enter">
</form>
</body>
</html>

 

 

<?php

foreach ($_POST['button'] as $key => $val) {
    $name[$key] = $val;
}
echo $name[0];
echo $name[1];
?>

 

Either should work.

<html>
<body>
<form action="2.php" method="post">
<input type="text"  name="button1">
<input type="text"  name="button2"> <!-- notice I changed this to 2, crazy how it works! -->
<input type="submit" value="enter" name="submit">
</form>
</body>
</html>

 

<?php
if (isset($_POST['submit'])) {
  $id=array(1,2); 
  foreach ($id as $val) {
      $name[$id] = $_POST['button'.$id];
  }
  echo $name[1];
  echo $name[2];
}
?>

 

 

<?php
if (isset($_POST['submit'])) {
  foreach ($_POST['button'] as $key => $val) {
      $name[$key] = $val;
  }
  echo $name[0];
  echo $name[1];
}
?>

 

<html>
<body>
<form action="2.php" method="post">
<input type="text"  name="button[]">
<input type="text"  name="button[]">
<input type="submit" name="submit" value="enter">
</form>
</body>
</html>

 

Is the form and the php code on the same page?  If so try the above as what was happening is it was trying to access the data with no data, which generally does not play well.

Archived

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

×
×
  • Create New...

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.