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>
asked Jun 5, 2019 at 21:07
2
  • pretty much that indeed : so I want array_articles to fill values article, Ean, brand, ref with the related data Commented Jun 5, 2019 at 21:16
  • So why don't you just construct $array_articles to be in the format that you want in the first place? Commented Jun 5, 2019 at 21:22

1 Answer 1

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

answered Jun 5, 2019 at 21:17
Sign up to request clarification or add additional context in comments.

11 Comments

hey man, thanks for your answer for I tried something like var json = '<?php echo json_encode($temp_articles) ?>'; var array_articles = JSON.parse(json); console.log(array_articles.narticle); but it throws me an error SyntaxError: JSON Parse error: Expected '}'
Ok let me think again.. Please wait! 😅
Hey is your file is in .js / .html format? I think you are declearing all the <?php ... ?> as a string of the json.
it's a .php file :)
For my case it's working... Please try it again in another way! Thanks!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.