Jump to content



Photo
- - - - -

Help ! With Chapter 15 Php And Javascript Together

view.js adaptation php javascript

  • Please log in to reply
4 replies to this topic

#1 sam67

sam67

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 29 April 2017 - 8:54 AM

Hi Larry

my name is sam can i first start by saying i absolutely love your books. i am currently working on a project where i have built the php side of things and now working on enhancing using javascript. i am adapting your script from your Modern javascript develop and design book on chapter 15, (view.js).

But the problem i have is my php script loops through the database to show results and i am trying to pass the id of the data, like you do in your view.php script to the view.js script with,

<script>

var itemId = ' . $itemid .'; 

</script>

 

BUT i have a while loop on the php script and the script tags are in the while loop like this,

 

$query = "SELECT `item_id` FROM items";

$results = mysqli_query ($dbc, $query);

 

while (list ($item_id ) = mysqli_fetch_array($results, MYSQLI_NUM))  { // Start the while Loop here ...

 
 echo '<form action="" method="post" id="bidForm" class="bidForm">
<input class="button" type="submit" value="Like" title="Like">
<input type="hidden" name="item_id" id="item_id" class="video" value="' . $item_id . '">

 

</form>'; ?>
<script>
var itemId = "<?php echo $item_id; ?>";
</script>

 

} // End of while Loop here .. then link to external js script 

 

<script src="js/ajaxBid.js"></script>

 

Just so you know this is not exact just example above but it does the exact same thing. So the problem is where it Loops the var itemId gets more then one value so you don't get the value of the button clicked sent to the ajax script. How can i get the id of the button clicked in my js script while still in the while loop????

sorry if this is really confusing i will answer any questions you might have.

if you or anyone could explain in the simplest possible way where I'm going wrong as not brilliant at javascript yet, that would be amazing been stuck for ages on this.

many thanks,

Sam


  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4758 posts
  • LocationState College, PA (USA)

Posted 11 May 2017 - 3:42 PM

Sorry for the delayed reply! I'm assuming this is the same issue as http://larryullman.c...89-help-please/

 

First, I'm pretty sure you don't want to create the FORM once for each loop. What I would do is create each button as a link instead, of the format somepage.php?id=X, where X is populated by the PHP code. Put a class on the link, too. Then you'd add JavaScript code that interrupts clicks on links of that class and performs an Ajax request instead. 

 

Does that make sense?


  • 0

#3 sam67

sam67

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 18 May 2017 - 8:16 AM

hi 

yeah thanks i think i have figured it out.

 

i have one more question, I copyed a tutorial on youtube the link to it is below:

 

It's ajax page loading, so you dont have to reload a different page.  

i got it to work. But on one page i have json data which does not load when you click on the page anymore.

it works when you refresh the page but that defeats the purpose of ajax page loading,

my question is:

How can i load a page that has json data on it with out refreshing the page ???

 

many thanks for all the help i really appreciate it,

sam.


  • 0

#4 sam67

sam67

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 18 May 2017 - 8:18 AM

P.S. there are 2 parts to the video above.


  • 0

#5 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4758 posts
  • LocationState College, PA (USA)

Posted 23 May 2017 - 3:52 PM

The process should be the same except you should be providing JSON as the content type when it's sent from the server and expecting JSON as the content type on the page. Could you provide some code or a URL where I could see this? 


  • 0