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.

 

 

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.