0

I have a select box in my form and inside that select box there's a list of a,b,c and others.and I have a input type="text" which is disabled.now, when I select others in select box the input will be enabled so that the user can type anything that cannot be found in the select item.this function works fine but, the only problem is that when I try it now to update its not submitting..can anyone help me..

html

<Select name="category" id="category" class="form-control" onChange="category();">
    <option value="<?php echo $a; ?>" selected="selected">Please Select a Category</option>
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
    <option value="OTHERS">Other/s</option>
</select>

<input type="text" id="Others" name="Others" value="<?php echo $Others; ?>" class="form-control" placeholder="Other/s" disabled="true">

javascript

$(function(){
    $("#category").change(function(){
        if($(this).val()=="OTHERS"){
            console.log(true);
            $("#Others").removeAttr("disabled");
        }
        else{
            console.log(false);
            $("#Others").attr("disabled", "disabled");
        }
    });
});

php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $ID = $_GET['ID'];
    $Category = $_POST['Category'];
    $Others = $_POST['Others'];

    if($LTS->update($ID,$Category,$Others)){
        echo "<script type='text/javascript'>alert('Successfully Updated!');</script>";
    }
    else {
        echo "<script type='text/javascript'>alert('Updating Failed!');</script>";
    }
}

if(isset($_GET['ID'])) {
    $ID = $_GET['ID'];
    extract($LTS->getID($ID)); 
}
?>

class.user.php

public function update($ID,$Category,$Others) {
    try{
        $stmt = $this->db->prepare("UPDATE sfund SET Category = :Categoryy,Others = :Others WHERE ID = :ID");
        $stmt->bindparam(":Category", $Category);
        $stmt->bindparam(":Others", $Others);

        $stmt->bindparam(":ID", $ID);
        $stmt->execute();
        return true;
    }
    catch(PDOException $e) {
        echo $e->getMessage();
        return false;
    }
}
Monasha
  • 711
  • 2
  • 16
  • 27
kie
  • 3
  • 3
  • 2
    $(#Others") needs a quote after the first parenthesis – user2182349 Nov 14 '16 at 01:25
  • oh, i'm sorry its just a typo..i updated my question – kie Nov 14 '16 at 01:30
  • what do you mean by not submitting? is it ajax? – Mark Vincent Manjac Nov 14 '16 at 01:38
  • `$("category")`, you're forgetting something here.. Should be `$("#category")` (missing `#`) – Qirel Nov 14 '16 at 01:39
  • You need to add a **#** in front of category on the selector – user2182349 Nov 14 '16 at 01:39
  • the disabled form input not submitting data. but when i tried to remove the disabled="true" in my input it successfully updated but with disabled is true the updating is failed – kie Nov 14 '16 at 01:41
  • its a typo again.sorry for that – kie Nov 14 '16 at 01:43
  • If the inputs are disabled, they won't be submitted, as they are *disabled*. http://stackoverflow.com/questions/7357256/disabled-form-inputs-do-not-appear-in-the-request so that's working as intended. So with your code, if the function is working, the input won't be active unless you select `OTHERS` – Qirel Nov 14 '16 at 01:44
  • @Qirel yeah thats right so, what should i do to fix it? – kie Nov 14 '16 at 01:48
  • I'm not really sure what you want to achieve, so I'm not sure what you want to "fix". That being said, if you want the input to be submitted regardless, but only editable if the select is set to `OTHERS`, just make it `readonly` instead of `disabled`. – Qirel Nov 14 '16 at 01:51
  • @Qirel i already saw link you posted but if change the disable to readonly the user can't change it right? – kie Nov 14 '16 at 01:51
  • why not instead of removing and adding the attribute `disabled`, try making it `true` or `false` like `$("#Others").prop("disabled",true);` and `$("#Others").prop("disabled",false);` – Mark Vincent Manjac Nov 14 '16 at 01:58
  • @MarkVincentManjac i'll your code..wait.. – kie Nov 14 '16 at 01:59
  • @MarkVincentManjac tried your code but its not working..i think there's something wrong with my code..can you please help me edit the above code.. – kie Nov 14 '16 at 02:02
  • so the input and select tags from above is inside the form tag? – Mark Vincent Manjac Nov 14 '16 at 02:02
  • @MarkVincentManjac yeah – kie Nov 14 '16 at 02:05
  • what is your code in submitting the form? – Mark Vincent Manjac Nov 14 '16 at 02:15
  • wait i'll edit my code – kie Nov 14 '16 at 02:18
  • i'll already updated my above code – kie Nov 14 '16 at 02:23
  • If the field is not supposed to be modified by the user than you should be careful when using it on the server-side. Anyone can make a request to your server and fake the values to be whatever they like. Or they can just use the developer console to change values. –  Nov 14 '16 at 02:24
  • @MarkVincentManjac if my code doesn't work on you maybe you could just make your own sample.. – kie Nov 14 '16 at 02:25
  • why not make a short hand ajax request to pass the value from your form to the php. – Mark Vincent Manjac Nov 14 '16 at 02:28
  • @Terminus lets say a user select "OTHERS" because the user did not find the item he's looking for of course he will select OTHERS and a input text will be enabled so that the user can input what he want to input there. – kie Nov 14 '16 at 02:28
  • @MarkVincentManjac how can i do that..i'm sorry for my question, i'm just a newbie in javascript – kie Nov 14 '16 at 02:29
  • I've posted my own solution. Just ask me if you have questions in my answer. – Mark Vincent Manjac Nov 14 '16 at 02:41

1 Answers1

0
$('#submitButton').click(function() {
  var others = $('#Others').val();
  var category = $('#category').val();
  $.post('script.php', {
    action: 'submit',
    others: others,
    cat: category
  });
});

script.php is the name of your php file. include the path if it is inside the folder.
In your PHP, change $_SERVER["REQUEST_METHOD"] == "POST" with
isset($_POST['action']) && $_POST['action'] == "submit" to separate it from other ajax that you will create.

Mark Vincent Manjac
  • 507
  • 1
  • 6
  • 27