Trouble with forms - $_POST['']

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Takeshi
New php-forum User
New php-forum User
Posts: 7
Joined: Fri May 17, 2013 3:07 am

Trouble with forms - $_POST['']

Postby Takeshi » Fri May 17, 2013 3:14 am

Hi all, I have little-big problem :-) I need to do something like this ...

Code: Select all

<?php $array = array("aaa","bbb","ccc","ddd","eee","fff","ggg"); ?>
<?php foreach($array as $arr){ ?>
<form method=post name=my_form>
<a href="#" name="point" onclick="$(this).closest('my_form').submit()">
    <?php echo $arr ?>
</a><br>
</form>
<?php
}
    if(isset($_POST['my_form'])){
        echo $_POST['point'];
    }
?>


... so ... There is an array -> I want to use something like <a link></a> as submit button and then using $_POST method write its value ... but something is wrong ... can you help me please? Thanks a lot! :-)

Takeshi
New php-forum User
New php-forum User
Posts: 7
Joined: Fri May 17, 2013 3:07 am

Re: Trouble with forms - $_POST['']

Postby Takeshi » Fri May 17, 2013 4:35 am

OK so I tried to get value of a link ... but as I see now, this method was not good ...
.... Now ... I use javascript instead of $_POST and it works perfect ...so this is my ne wersion of code .. :)

Code: Select all

<?php $array = array("aaa","bbb","ccc","ddd","eee","fff","ggg"); ?>
   <?php foreach($array as $arr){ ?>
      <a href="#"onclick="alert( this.firstChild.nodeValue );return false;">
         <?php echo $arr ?>
      </a>
<?php } ?>

User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Re: Trouble with forms - $_POST['']

Postby sandeep605085 » Fri May 17, 2013 4:57 am

Hi,

If you are trying to post the form to the same page and to get the value of the clicked linked from the posted form then the following code might help you.

and also you need to include the jquery.js to your code to get $(this) to be workable.

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' type="text/javascript"></script>
<?php
$array = array("aaa","bbb","ccc","ddd","eee","fff","ggg");

foreach ( $array as $arr ) {
?>
<form method='post' name='my_form' action='<?php echo $PHP_SELF;?>'>
<input type='submit' name="point" onclick="$(this).closest('my_form').submit()" value='<?php echo $arr ?>' />
<br>
</form>
<?php
}
if ( isset($_POST['point']) ) {
echo $_POST['point'];
}
?>


I have changed the anchor(a) link to input button to submit the form.
Hope this helps you what you need.

Thanks,
Sandeep Agrawal
Mindfire Solutions.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Trouble with forms - $_POST['']

Postby johnj » Fri May 17, 2013 6:00 am

@Takeshi, you are having <form> inside a loop which means you have more than one form but your form name for all these forms is my_form. Try to use different/unique form names.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest