Jump to content

form field array question


dtdetu

Recommended Posts

hello i use this checkbox code

 

  <?   
$q=mysql_query("select * from iss");
echo "    
    
          <table border=0 cellspacing=5 width=80%><tr valign=top><td width=33%>";
$i=1;
while($r=mysql_fetch_array($q))
{
    
   
    echo "<input type=checkbox name=is[]  value=$r[id] >$r[isadi]<br>";       
    if($i==17) {
        echo   "</td><td width=33%>";
        $i=0;
    }
    $i++;
}
echo "</td></tr></table>";
?>

it works but i want to add some kodu to auto check if the post value is checked i used

 

  <?   
$q=mysql_query("select * from iscesitleri");
echo "    
    
          <table border=0 cellspacing=5 width=80%><tr valign=top><td width=33%>";
$i=1;
while($r=mysql_fetch_array($q))
{
    
   if($_POST[is][1] == $r[id])
{
$checked = "checked";
}
    echo "<input type=checkbox $checked name=is[]  value=$r[id] >$r[isadi]<br>";       
    if($i==17) {
        echo   "</td><td width=33%>";
        $i=0;
    }
    $i++;
}
echo "</td></tr></table>";
?>

but it didnt work can anyone help please

Link to comment
Share on other sites

There are a few BASIC errors in your script and some questions that I have:

 

1) Never start PHP with "" always use "<?php".

2) Where is your form declared?  You need something like this if you want to submit the form to itself.

</pre>
<form method="POST" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>"><

3) Show us all the code.

4) So basically you want to generate check boxes via a DB, submit it and remember what was checked after submission?

Link to comment
Share on other sites

First off Maq was just trying to help you without even seeing much of your code. he is trying to show you the proper methods of php. Basically what I see is wrong is that you did not declare $checked. You need to add this:

 

$checked = "";

 

above this:

 

echo "<input type=checkbox $checked name=is[]  value=$r[id] >$r[isadi]<br>";   

 

and change this:

 

echo "<input type=checkbox $checked name=is[]  value=$r[id] >$r[isadi]<br>"; 

 

to this:

 

echo "<input type=checkbox checked=$checked name=is[]  value=$r[id] >$r[isadi]<br>"; 

 

That way it is defined either way.

Link to comment
Share on other sites

dont ruin my topic if you dont know the answer please you said something none of them are usefull for me

 

It may not be useful to you, but it's useful to me.  I don't know how your script works, you're the one who's too inept to write a basic PHP script.  So don't worry about me, "ruining" your topic, because I don't like helping ignorant people that are asking for help.

 

Think about how vague your question is

 

but it didnt work can anyone help please

 

You need to learn how to ask a question correctly or no one will be able to help

 

What the hell is "kodu" anyway?!

Link to comment
Share on other sites

did you try this?

 

and change this:

 

Code: [select]echo "<input type=checkbox $checked name=is[]  value=$r[id] >$r[isadi]<br>";

 

to this:

 

Code: [select]echo "<input type=checkbox checked=$checked name=is[]  value=$r[id] >$r[isadi]<br>";

 

if so are you getting any errors or what is happening?

Link to comment
Share on other sites

did you try this?

 

and change this:

 

Code: [select]echo "<input type=checkbox $checked name=is[]  value=$r[id] >$r[isadi]<br>";

 

to this:

 

Code: [select]echo "<input type=checkbox checked=$checked name=is[]  value=$r[id] >$r[isadi]<br>";

 

if so are you getting any errors or what is happening?

 

yes i tried this now it shows every value as checked

Link to comment
Share on other sites

maq get lost and ruin another topic i wont be bothered with you

 

Funny how you can't even figure out the solution to a basic PHP technique that I have sitting right in front of me all because you're an ignorant ass.

 

P.S. - Your code is out of date and probably has many security holes and exploits, have fun with that one jerkoff.

Link to comment
Share on other sites

hey idiot Maq its non of your business

 

None of my business?  If it's such a secret why are you posting on a public forum...?  I think you're the idiot.  Besides you're the one in search of answers, I already have them.

 

edit: removed profanity from quote

Link to comment
Share on other sites

You really should stop hating on Maq because he is right about alot of things that he has told you and was just trying to be helpful. He is a positive influence on this community I must say myself. If you are going to be a part of this community you must be open to constructive criticism. Anyways this should help:

 

  <?   
$q=mysql_query("select * from iscesitleri");
echo "    
    
          <table border=0 cellspacing=5 width=80%><tr valign=top><td width=33%>";
$i=1;
while($r=mysql_fetch_array($q))
{
    
?>

<input type="checkbox" <?php if($_POST[is][1] == $r[id]){ echo "checked='$checked'"; } ?> name="is[]"  value="<?php echo $r[id]; ?>" ><?php echo $r[isadi]; ?><br>
       
<?php
    if($i==17) {
        echo   "</td><td width=33%>";
        $i=0;
    }
    $i++;
}
echo "</td></tr></table>";
?>

 

I think that should get it going. Please let me know if it works and if you get an error let me know what it is.

Link to comment
Share on other sites

<?php

$query = "select * from iss";
$result = mysql_query($query );

echo "<table border=\"0\" cellspacing=\"5\" width=\"80%\"><tr valign=\"top\">\n";

$records_per_column = ceil(mysql_num_rows($result)/3);
$current_record = 1;

while($record=mysql_fetch_array($result))
{
    if ($current_record==1)
    {
        echo "<td width=33%>\n";
    }

    $checked = (in_array($record['id'], $_POST['is'])) ? ' checked="checked"' : '' ;
    echo "<input type=\"checkbox\" name=\"is[]\"  value=\"{$record['id']}\"$checked />{$record['isadi']}<br>\n";       
    $current_record++;

    if($current_record > $records_per_column)
    {
        echo "</td>\n";
        $current_record = 1;
    }
}

if($current_record != 1)
{
    echo "</td>\n";
}

echo "</tr></table>";

?>

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.