1

I make edit.php with show all data in form from MySQL.
All data is show on form rightly, but it's not work on dropdown and textarea.

I need help and this is my code

<form method="post" action="editdata.php">
<?php 
  include 'config.php';
  $id = $_GET['id'];
  $sqlTampil = "select * from data_korban Where kasus_id=$id"; 
  $qryTampil = mysql_query($sqlTampil); 
  $dataTampil = mysql_fetch_array($qryTampil); 
?>  

Dropdown value is still default, not selected value and TextArea is blank

<select name="agama" id="agama" value="<?php    echo $rows -> agama;?>">
    <option value="Islam">Islam</option>
    <option value="Khatolik">Khatolik</option>
    <option value="Protestan">Protestan</option>
    <option value="Hindu">Hindu</option>
    <option value="Buddha">Buddha</option>
    <option value="Lain-Lain">Lain-Lain</option>
</select>

<textarea id="alamatkorban" rows="5" name="alamatkorban" 
          value="<?php echo $rows -> alamatkorban;?>" 
          cols="33">
</textarea>

Thank You for Your Help

4

6 Answers 6

7

Your biggest issue is you are accessing your database values incorrectly. mysql_fetch_array() does not return an object. It returns an array. So you use array syntax ($rows['key']) not object syntax ($rows->key).

Just check to see if the option value matches the value of $rows['agama']. If so, add the selected attribute.

<select name="agama" id="agama">
    <option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
    <option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
    <option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
    <option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
    <option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
    <option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>

An even better way would be to put all of your options in an array and loop through them to generate your options. Then you can check their values as you loop through them. This would be less code an easier to maintain.

<select name="agama" id="agama">
<?php
$agamas = array('Islam', 'Khatolik', 'Protestan', 'Hindu', 'Buddha', 'Lain-Lain');
foreach ($agamas as $agama) {
      $selected = ($rows['agama'] === $agama) ? ' selected="selected"' : '';
?>
    <option value="Islam"<?php echo $selected; ?>>Islam</option>
<?php
}
?>
</select>

To fix your textarea issue, <textarea> does not have a value attribute. You need to place the content in between the <textarea></textarea> tags:

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php echo $rows['alamatkorban'] ;?></textarea>
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your help, but the result is: value for the dropdown still default. I have selected Hindu for Agama, when I try to edit, all form is work correctly, dropdown is show default value and textarea is blank ...
If you do `<?php echo $rows['agama']; ?> what do you see?
2

Okay let us assume that there is a variable that holds the selected value and we name it $selected and the options for our select will be stored in $options.

$selected = "Buddha" ;
$options  = array('Islam', 'Khatolik', 'Protestan', 'Hindu', 'Buddha', 'Lain-lain');

In your edit.php file you should try creating the select element via php echo

<?php
    foreach($options as $option){
        if($selected == $option){
            echo "<option selected='selected' value='$option'>$option</option>" ;
        }else{
            echo "<option value='$option'>$option</option>" ;
        }
    }
?>

Comments

0

the option that you want selected needs to have the "selected" property in the option tag.

Islam.

The content of the textarea should exist inside of the open/close e.g.

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php    echo $rows -> alamatkorban;?></textarea>

Comments

0

You had your textarea declared value wrong. There's no value tag for textarea. What you need to put to your textarea is like this:

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php echo $rows -> alamatkorban; ?></textarea>

2 Comments

I use textarea for long text, it's work correctly when I using form. There is any suggestion for change the textarea? Thank You
You successfully added some value to your textarea by using value tag?
0

Looks like you've got a couple problems: As mentioned, <textarea> tags don't use the value property, but rather you change the inner HTML, so you have:

    <textarea>Text inside the text area is written here, like <?php echo $var; ?></textarea>

Your other problem is because you need a 'Selected' option inside the tag for the option you want selected by default. So:

    <select id="selector">
<?php
$optionArray=array("Option 1","Option 2","Option 3");
foreach ($optionArray as $option){?>
    <option id="<?= $option? >"<? if ($rows[$option]==$option){ echo " selected"; } ?>><?= $option ?></option>
<?}?>

Should do it - that way, you can keep all of your options in an array that is simply looped through. The <?= ($var) ?> tags are php short tags equivalent to <?php echo ($var); ?> to keep things a little shorter.

Comments

0

You may try this...textarea should exist inside of the open/close e.g.

<textarea rows="5" cols="33" id="alamatkorban" name="alamatkorban" autofocus autocomplete="off"><?php echo @$row["alamatkorban"]; ?></textarea>

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.