1

There is a HTML element of type text whose name is fiche_tache_ref. Inside a javascript file (.js) outside of the page's folder I want to set its value according to the value of a listbox selected :

function changerNatureRefTache(nature_tache_id) { // nature_tache_id is from a listbox

    var http_ajax = $('#http_ajax').val();
    var html = $.ajax({
    data: "nature_tache_id="+nature_tache_id ,
    type: "POST",
    url: http_ajax+"referentiel/AjaxTacheChangerNatureTache.php" ,  
    async: false
    }).responseText;

        // here I want to set the value of the field

}

Code of AjaxTacheChangerNatureTache.php is :

<?php
    if (isset($_POST['nature_tache_id'])) {
        define("ROOT_PATH", "../../");
        require_once ROOT_PATH  . 'config.inc.php';
        require_once RP_MODELS  . 'nature_tache.class.php';
        $db     = new DbConn();
        $obj    = new nature_tache($db->getInstance());
        $ret    = $obj->recupererNatureTache($_POST['nature_tache_id']);
        $code   = $ret['nat_code'];
        echo $code;
    }
?>

So how to set the value of the field using JQuery ?

4 Answers 4

4

Use the val() function

$('#idofinput').val("text you want in the field");

Since you are using name you can do the following

$('input[name=fiche_tache_ref]').val("text you want in the field");

Since names are not necessarily unique be careful with using the name as a selector. I would generally advise using an id instead.

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

1 Comment

You beat me by a minute.. :-)
2

I dont get you correctly but something like this is used to set the value of input element in jquery:-

$("#elementid").val("myvalue");

Comments

1

Use the val() function of Jquery.

$('#IdOfTextbox').val("Some Text");

Further Reference

Comments

0

you'll get lots of the same answer to this question, because this is kind of the low hanging fruit in the jquery docs.

$("selector").val("new value");

but one tip I'll give you that will make your php a little cleaner is instead of defining a root path like this:

define("ROOT_PATH", "../../");

you can set an include path in an .htaccess file using the following code:

php_value include_path ".:/path/to/your/application"

and then you can require files relative to that path (i.e. require_once('config.inc.php') as opposed to what your essentially doing, which is require_once('../../config.inc.php').

The .htaccess file should live in the main directory that holds your application.

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.