1

I have textbox that are query from database. I show this textbox from while loop.

$ans_no = 0;
 while($ed_ans_row = mysql_fetch_array($ed_ans_query)) {
   $edit_answer = $ed_ans_row['name'];
   $edit_id = $ed_ans_row['id'];
   echo "<input type=\"text\" name=\"ans[]\" class=\"ans\" value=\"$edit_answer\" /><br/>";
   echo "<input type=\"hidden\" name=\"edit_id[]\" class=\"ans\" value=\"$edit_id\" />";
  }

Now, I want to save this pair of answer and id into database like this:

UPDATE table SET answer='ans[0]' WHERE id='edit_id[0]';

But I don't know how many textbox are query from database. And how to update this answer and id together correctly.

I don't have a lot of experience in php. Thanks for every reply.

5
  • what is the value of $ans_no?? Commented Nov 20, 2015 at 9:13
  • you can post form to get value.. and also use ajax for this Commented Nov 20, 2015 at 9:14
  • I don't want to use ajax Commented Nov 20, 2015 at 9:16
  • how about name=\"ans-".$edit_id."\". it will be easier later on when trying to save. Commented Nov 20, 2015 at 9:23
  • yes, but I want to know how to I retrieve this each of ans-$edit_id. because ans- is static and $edit_id is different for each other. right? Commented Nov 20, 2015 at 9:29

2 Answers 2

3

Create your text box with names name[]

<input type='text' name='name[]'>

Get values like that :-

$name= $_POST['name'];
$N = count($name);
for($i=0; $i < $N; $i++)
{
echo($name[$i]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

And how do I add this array into each variable. Please can you show me sample. Because I don't have a lot of experience in php. Thank you.
$name= $_POST['name']; $N = count($name); for($i=0; $i < $N; $i++) { echo($name[$i]); }
get value like that @NightMare
0

You can say:

$i = 0;
$answers = array();
while(isset($_POST['ans-'+$i])){
    $answers[$i] = $_POST['ans-'+$i];
    $i++;
}
print_r($answers);

Comments

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.