Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

from php array to javascript (beginner) [duplicate]

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>

Answer*

Draft saved
Draft discarded
Cancel
11
  • 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 '}' Commented Jun 5, 2019 at 21:42
  • Ok let me think again.. Please wait! 😅 Commented Jun 5, 2019 at 22:08
  • Hey is your file is in .js / .html format? I think you are declearing all the <?php ... ?> as a string of the json. Commented Jun 5, 2019 at 22:10
  • it's a .php file :) Commented Jun 5, 2019 at 22:15
  • For my case it's working... Please try it again in another way! Thanks! Commented Jun 5, 2019 at 22:15

default

AltStyle によって変換されたページ (->オリジナル) /