0

i want to get the text box value which i selected from array of text boxes.. My code is like this:

Here am displaying the data on the browser from DB..

<?php
foreach($dataDecoded['categories'] as $key => $value)
{
?>
<tr>
<td><?php echo $value['CategoryName'];?></td>

<td>
<input type="text" name="categoryId[]" id="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName[]" id="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" id="create" value="create">
</td>
</tr>
<?php
}
?>

The data will be displayed like:

categoryId1     categoryName1    <Create Button>

categoryId2     categoryName2    <Create Button>

categoryId3     categoryName3    <Create Button>

and so on like this..

Now, suppose When i click on Create Button of CategoryName2, i want to get the categoryId and categoryName of only CategoryName2..

Am using this code:

if(isset($_POST['create']) && $_POST['create']=="create")
{
    $cat_id[]=$_POST['categoryId'];
    $cat_name[]=$_POST['categoryName'];
}

But with this, am getting all the categoryId and categoryName into the array.. how to get only the selected textbox value..

I want to do it using only PHP and not with JavaScript / JQuery... Am a bit new to PHP... Can someone help me / give me a suggestion about how to do it...

Thanks in advance.

1
  • 1
    Why don't you just create a form for each category ? Commented Jun 13, 2015 at 11:46

1 Answer 1

1

Wrap each one in its own form:

<td>
    <form method="POST" action="somePage.php">
        <input type="text" name="categoryId" value="<?php echo $value['CategoryId']?>">
        <input type="text" name="categoryName" value="<?php echo $value['CategoryName']?>">
        <input type="submit" name="create" value="create">
    </form>
</td>

Since the content being posted in this case is just these two values, then that's the entire scope of a single form post. So each one is logically/semantically its own form. (There's no rule that says a page can have only one form. Unless you're ever using ASP.NET WebForms, which is a lie.)

Note also that I removed your id attributes. Multiple elements can't have the same id, that's invalid HTML and any behavior as a result becomes undefined.

Sign up to request clarification or add additional context in comments.

5 Comments

But i have around 2000 categories in my DB.. won't the code be heavy if i do so ???
@phpfresher: Having 2000 of anything on a single webpage is probably a less than ideal UX, you might look into something more dynamic than that. But what makes you think one extra element per item is going to make a huge difference? You already have 6 HTML elements per category, is 7 really going to be a problem?
Thank You.. It worked for me... :) I'll go with it... But is there any other solution other than taking one form for each category ?? coz, if in some other place i have the same problem with some lakhs of records, it'll be a problem there naa.. :)
@phpfresher: Another approach (which I haven't tried, but it might work) could be to set the value of the submit button to the Category ID. Even though the entire form is submitted, I think only the clicked submit button is included in the form values. Though even if that works, it's unnecessary overhead to submit thousands of values when you only need one value. You're worrying too much about the in-browser performance and sacrificing API performance as a result. A few more lines of code won't hurt anything if the code logically makes more sense.
okkk.. thank you.. :-) I've wasted my whole day for this code.. It makes me laugh when i think i wasted my whole day for only 7 lines of code.. hahahahaa...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.