2

so basically I'm looking into javascript and jquery because it looks like I could use it in my projects.

Today I tried to get a bit more into it : I have a database filed with articles id, Ean codes, brands and references. What I would like to do is : you type the article number in one input and it fills the 3 others.

I looked around a bit and frankensteined my very first own code. and it works for the most of it. What I can't figure out is how to fill const values with the data from the array I get from my database instead of the arrays I set up myself. I tried several things but yeah I lack the basics so I guess it will be trivial for you guys

Thanks in advance for your help!

    <?php

$sql = "SELECT * FROM prescreen_articles";
$result = mysqli_query($conn, $sql);

while ($articles = mysqli_fetch_array($result)){

    $temp_articles[] = array(
        "narticle" => $articles['article_number'],
        "ean" => $articles['EAN'],
        "brand" => $articles['brand'],
        "ref" => $articles['ref']
    ); 
}

?>



<input name="artible" id="article" type="text">
<input name="ean" id="ean" type="text" disabled>
<input name="brand" id="brand" type="text" disabled>
<input name="ref" id="ref" type="text" disabled>

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script>
    jQuery(document).ready(function() {

        const $main = $('#article');
        const $ean = $('#ean');
        const $brand = $('#brand');
        const $ref = $('#ref');

        var id;
        var array_articles = <?php echo json_encode($temp_article); ?>;

        const values = {
          article: ['2222222', '9999999', '1111111'],
          ean: ['1', '2', '3'],
          brand: ['samsung', 'apple', 'huawei'],
          ref: ['S10', 'xr', 'P30'],

        };

        $main.on('keyup', function () {

            if($main.is(':empty') || $main.lenght < 7){
                ean.value = '';
                brand.value = '';
                ref.value = '';
            }

            if (jQuery.inArray(this.value , values.article) !== -1) {
                id = jQuery.inArray(this.value ,values.article);
                ean.value = values.ean[id];
                brand.value = values.brand[id];
                ref.value = values.ref[id];
            }
        });
    });     
  </script>
elazard
  • 48
  • 6
  • pretty much that indeed : so I want array_articles to fill values article, Ean, brand, ref with the related data – elazard Jun 05 '19 at 21:16
  • So why don't you just construct `$array_articles` to be in the format that you want in the first place? – Patrick Q Jun 05 '19 at 21:22

1 Answers1

1

[PHP] array -> JSON

json_encode(your_array);


[JS] JSON -> JS Object

JSON.parse(your_json);

Hope that will work for you. If you have further problem, please let me know in the comment

Jawad Ahbab
  • 1,462
  • 1
  • 19
  • 31
  • hey man, thanks for your answer for I tried something like `var json = ''; var array_articles = JSON.parse(json); console.log(array_articles.narticle);` but it throws me an error SyntaxError: JSON Parse error: Expected '}' – elazard Jun 05 '19 at 21:42
  • Ok let me think again.. Please wait! – Jawad Ahbab Jun 05 '19 at 22:08
  • Hey is your file is in `.js / .html` format? I think you are declearing all the `` as a string of the json. – Jawad Ahbab Jun 05 '19 at 22:10
  • it's a .php file :) – elazard Jun 05 '19 at 22:15
  • For my case it's working... Please try it again in another way! Thanks! – Jawad Ahbab Jun 05 '19 at 22:15
  • Here you can check this [see only code, fiddle will not work] https://jsfiddle.net/hc95dLom/ – Jawad Ahbab Jun 05 '19 at 22:16
  • well no I don't know I mean I ve been trying for a while now and I can't make it work. how would you approach what I am trying to do? basically I have a database with 4 columns article_id, Ean, brand, ref and I would like to have 4 input. Only the first one you can fill with the article number and when it matches one article number from the database javascript fills the 3 other inputs. it sounds simple enough but I can't make it work for the life of me. thanks man – elazard Jun 05 '19 at 22:50
  • Ok, No problem, If you can't I will try to solve your problem. I jst need to sleep for a while.! Stay connected! Till than try yourself – Jawad Ahbab Jun 05 '19 at 22:55
  • haha sure thing man I am not stopping yet (even if I am getting tired too lol) thanks a ton for your help :) – elazard Jun 05 '19 at 22:57
  • hey man I managed to do it :) thanks a ton for your help and being nice. in the end you your answer helped me get there so thanks again! (and basically what messed with my head was a lost = in my for loop) – elazard Jun 05 '19 at 23:27
  • Ha ha, Congratulation. Nice to meet you. Have a nice day!! – Jawad Ahbab Jun 06 '19 at 06:06