0

i have a problem retrieving data from mysql array, what i want to do is, when user open the same form next time for existing ID, he should see which option is checked, and which not, so, if he submit the form without changing anything (on checkbox), it'll be the same again.

Here's the code:

$media_array = $_POST['nesto1'];
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$nesto1 = substr($source, 0, -2);

<label><input type="checkbox" name="nesto1[]" value="NewData" /> NewData </label>
<label><input type="checkbox" name="nesto1[]" value="NewData2" /> NewData2 </label>
<label><input type="checkbox" name="nesto1[]" value="NewData3" /> NewData3 </label>

Here's how i retrieve the data:

$result = mysql_query("SELECT * FROM `article` WHERE id=".intval($_GET["id"])." ORDER BY id ASC");
$row = mysql_fetch_array($result);

How should i make checkbox option to be checked on next open, for example, NeData and NewData3, if i have values in my mysql row as: NewData, NewData3.

I have tried with in_array, but i didn't success and here's what i have tried:

<label><input type="checkbox" name="nesto1[]" value="NewData"<?php echo in_array("NewData",$nesto1)?" checked="checked"":""; ?> />NewData</label>
<label><input type="checkbox" name="nesto1[]" value="NewData2"<?php echo in_array("NewData2",$nesto1)?" checked="checked"":""; ?> />NewData2</label>
<label><input type="checkbox" name="nesto1[]" value="NewData3"<?php echo in_array("NewData3",$nesto1)?" checked="checked"":""; ?> />NewData3</label>

Thanks in advance.

New code (i have changed values, array and syntax error is fixed now):

if(isset($_POST["name"]) && isset($_POST["change"]) && isset($_POST["nesto2"]) && is_numeric($_POST["change"])){ $id = intval($_GET["id"]);

$name = mysql_real_escape_string($_POST["name"]);
$nesto1 = mysql_real_escape_string($_POST["nesto1"]);
$nesto2 = mysql_real_escape_string($_POST["nesto2"]);
$change = mysql_real_escape_string($_POST["change"]);
$type=  mysql_real_escape_string($_POST["category"]);
$media_array = array();
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$nesto1 = substr($source, 0, -2);

$result = mysql_query("SELECT * FROM `article` WHERE id=".intval($_GET["id"])." ORDER BY id ASC");
$row = mysql_fetch_array($result);

form:

<label><input type="checkbox" name="nesto1[]" value="Kutija"<?php echo in_array("Kutija",$nesto1)?" checked=\"checked\"":""; ?> />Kutija</label>
<label><input type="checkbox" name="nesto1[]" value="Punjač"<?php echo in_array("Punjač",$nesto1)?" checked=\"checked\"":""; ?> />Punjač</label>
<label><input type="checkbox" name="nesto1[]" value="Baterija"<?php echo in_array("Baterija",$nesto1)?" checked=\"checked\"":""; ?> />Baterija</label>

Error: Warning: in_array() expects parameter 2 to be array, null given in /home/smarthos/public_html/articles/article/edit.php on line 142

1
  • Is there anyone who can help me with this? Commented Jun 6, 2013 at 15:01

3 Answers 3

0

Syntax error:

in_array("NewData",$nesto1)?" checked="checked"":""
                            ^--       ^--     ^^---

You cannot use the same type of quotes inside a string as you quoted the entire string with. Try

in_array("NewData",$nesto1)?' checked="checked"':""
                            ^--                ^--

or

in_array("NewData",$nesto1)?" checked=\"checked\"":""
                                      ^--      ^--

instead.

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

3 Comments

When i change that, i'm getting the follow error: Warning: in_array() expects parameter 2 to be array, null given in /home/smarthos/public_html/articles/article/edit.php on line 142 />
so $nesto1 isn't set in your code. e.g. you'd need $nesto1 = $_POST['nesto1'].
I have updated OP with suggestion, but i still get the warnings... Warning: in_array() expects parameter 2 to be array, null given in /home/smarthos/public_html/articles/article/edit.php on line 142
0

In your foreach look, check the array value for data, if data exists render 'checked'.

foreach ( as $key=>$value) {
    echo '<label><input type="checkbox" name="nesto1[]" value="NewData"        

    //if data item is checked, render option as checked
    if ($nesto1 == true) {
        //this could also be done via CSS on the option element.
        echo'checked';
    }
    echo $value. 'NewData</label>';
}

That should get you on the right track.

Comments

0

In your first code block, $nesto1 is not an array, so you can't use in_array. Try:

$nesto1 = array();
foreach ($media_array as $one_media) {
    $nesto1[] = $one_media;
}

Also, as @marc-b points, there is a syntax error.

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.