0

Ok, So I have this Dynamic drop down that ends with the user selecting the final destination.

Once that destination is selected, I have multiple text areas that pop out displaying editable info for that destination like tags, description, hotels etc...

I am having trouble getting these editable text areas to save back to the database with the new information that was edited by the user.

I watched some tutorials using the form=post method but I just cant get it to work.

Here is an example of my javascript which grabs arrays from my ajax.php file and displays them in my html section with the #descbo...

//grabs from ajax.php and generates description for that specific destination
        jQuery(".wrap").on('change', '#destination', function() {
            var data = 'destinationid=' +jQuery('#destination :selected').val();
            jQuery.post("<?php echo plugins_url(); ?>/Destination_Drop_Down_Menu/ajax.php", data, function(data) {
                if(data.errorcode ==0){
                    jQuery('#descbo1').html(data.chtml);

                }else{
                    jQuery('#descbo1').html(data.chtml);

                }

            }, "json");

        });

Here is an example of how my ajax.php file works which generates the arrays based on the the previous examples and then displays the text area boxes.

// Gets the description info from the last destination select drop down menu
$destination_id = isset($_POST['destinationid']) ? $_POST['destinationid'] : 0;
if ($destination_id <> 0) {
$errorcoded1 = 0;
$strmsg = "";
$sqlD1="SELECT * from destination WHERE IDDestination= ". $destination_id . " ORDER BY name;";
$resultD1=mysql_query($sqlD1);
$contD1=mysql_num_rows($resultD1);
if(mysql_num_rows($resultD1)){
$chtmlD1 = '<div name="description" id="description">';
while($row = mysql_fetch_array($resultD1)){
    $chtmlD1 .='<textarea name="Description" cols="130" rows="10">' .  $row['description'] . '</textarea>';
}
$chtmlD1 .= '</div>';

echo json_encode(array("errorcode"=>$errorcodeD1,"chtml"=>$chtmlD1));
} else {
    $errorcodeD1 = 1;
    $strmsg = '<font style="color:#F00;">No Description available</font>';
    echo json_encode(array("errorcode"=>$errorcodeD1,"chtml"=>$strmsg));
}
}

And here is my html section with the Form method=post. And my php function that supposed to save the information but instead it just re lodes the page without saving anything.

 <form action="" method="POST">
        <table cellpadding="5" cellspacing="2" border="0">
        <h1 align="center">General Information</h1>
        <tr>
        <td>description:</td>
        <td><div class="wrap" id="descbo1"></div></td>
            </tr>
             <tr>
                <td>page title:</td>
                <td><div class="wrap" id="descbo2"></div></td>
            </tr>
            <tr>
                <td>Meta Keywords:</td>
                <td><div class="wrap" id="descbo3"></div></td>
            </tr><tr>
            <td>Meta Description:</td>
            <td><div class="wrap" id="descbo4"></div></td>
            </tr>
             <tr>
                <td>

                    <input type="hidden" name="addedit" value="" />
                    <input type="submit" value="submit" name="submit"  style="background:#e5f9bb; cursor:pointer; cursor:hand;" />
                </td>
            </tr>
    </table>
    </form>
</body>
</html>
<?php
//see if the form has been completed
if (isset($_POST['submit'])){
    $ID = $_POST['IDDestination'];
    $description = $_POST['description'];
         if($ID&&$description){
        $exists = mysql_query("SELECT * FROM destination WHERE IDDestination='$ID'") or  die("the Query could not be done ");
        if(mysql_num_rows($exists) !=0 ){
            //update the description
            mysql_query("UPDATE destination SET description='$description' WHERE   IDDestination='$ID'") or die ("Update could not be applied.");
            echo "successful";
    }
    }
}

?>

I have no idea what I am doing wrong.Also a beginner

Any help would be much appreciated.

user3436399
  • 67
  • 1
  • 7
  • http://stackoverflow.com/help/mcve –  Apr 21 '14 at 19:48
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 21 '14 at 19:52
  • First off, don't include a bunch of code which is not related to the problem you are having. It sounds like everything is working fine except for the form POST itself, so why muddle up your question and potentially deter people from answering because they don't want to look through all that code. Second, tell us what you have done to try to debug this. Where is the behavior varying from what you want it to do? Have you done `var_dump($_POST)` to make sure POST values are coming through as expected? Have you tracked value as they change in your code to see where you get unexpected results? – Mike Brant Apr 21 '14 at 19:55
  • You don't have a form element named `IDDestination` so in using `$ID`, your query will fail. – Funk Forty Niner Apr 21 '14 at 20:37

1 Answers1

-1

I might have a silly question but you use $_POST['IDDestination'] in the php and destinationid in the javascript.

Could it be related to that?

Did you check whether you ever enter that if in the php that makes the sql query?

Also shouldn't this $description = $_POST['description']; rather read $description = $_POST['Description']; ?

Trefex
  • 2,290
  • 16
  • 18