Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    40
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Help, displaying database content with button

    Hey,
    Need some php help. I am trying to make it so when the user clicks on one the the restaurant names, the page is filled with its information.
    I am having problems with getting arguments to pass. Here's my code on the bottom. What I am trying to do is have
    Code:
     '<button onclick="american($row1[0],$row2[0])">click</button>';
    something like that

    The problem is, that not even
    Code:
    echo '<button onclick="american("lolol")">click</button>';
    is working, It should change the title to lolol but nothing. When I just have
    Code:
    echo '<button onclick="american()">click</button>';
    and in the script something like(script right under this) it works and changes the title to "dew.....", so I don't understand why it wont pass the argument and change the title.
    Code:
    function american()
    {document.getElementById("title").innerHTML="dewdwedewd";}
    </script>
    Here is the full code.
    Code:
    <?php
    		//creates the query for the database
    			$query = "SELECT title FROM group12db.restaurant WHERE category = 0;";
    			$result = mysql_query($query);
    			if (!$result) die("Database access failed".mysql_error());
    			$rows = mysql_num_rows($result);
    			
    			//for loop to get all the data in the database
    			for ($j = 0; $j<$rows;++$j){
    				//creates query to get the paragraph data from the database 
    				$row = mysql_fetch_row($result);
    				$query1 = "SELECT paragraph1 FROM group12db.review WHERE r_id = ".$j.";";
    				$result1 = mysql_query($query1);
    				$row1 = mysql_fetch_row($result1);
    				$query2 = "SELECT paragraph2 FROM group12db.review WHERE r_id = ".$j.";";
    				$result2 = mysql_query($query2);
    				$row2 = mysql_fetch_row($result2);
    				
    
    
    				//displace  restaurant in the catagory
    				echo $row[0].'<br />';
    				//creates a button for all the restaurant and sends the parameters to the javascript to displace paragraph one and two
    			  echo '<button onclick="american($row1[0],$row2[0])">click</button>';
    			  // echo '<input type="button" onclick="alert(\.$row1[0].)"/>';		
    			}
    			?>
    Code:
    <script>
    
    function american(infoo,infoos)
    {
    	//alert("Welcome " + title);
    document.getElementById("mainInfo").innerHTML=infoo;
    document.getElementById("title").innerHTML=infoos;
    
    }
    </script>

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by cpamieta View Post
    Hey,
    Need some php help. I am trying to make it so when the user clicks on one the the restaurant names, the page is filled with its information.
    I am having problems with getting arguments to pass. Here's my code on the bottom. What I am trying to do is have
    Code:
     '<button onclick="american($row1[0],$row2[0])">click</button>';
    something like that

    The problem is, that not even
    Code:
    echo '<button onclick="american("lolol")">click</button>';
    is working, It should change the title to lolol but nothing. When I just have
    Code:
    echo '<button onclick="american()">click</button>';
    and in the script something like(script right under this) it works and changes the title to "dew.....", so I don't understand why it wont pass the argument and change the title.
    Code:
    function american()
    {document.getElementById("title").innerHTML="dewdwedewd";}
    </script>
    Here is the full code.
    Code:
    <?php
    		//creates the query for the database
    			$query = "SELECT title FROM group12db.restaurant WHERE category = 0;";
    			$result = mysql_query($query);
    			if (!$result) die("Database access failed".mysql_error());
    			$rows = mysql_num_rows($result);
    			
    			//for loop to get all the data in the database
    			for ($j = 0; $j<$rows;++$j){
    				//creates query to get the paragraph data from the database 
    				$row = mysql_fetch_row($result);
    				$query1 = "SELECT paragraph1 FROM group12db.review WHERE r_id = ".$j.";";
    				$result1 = mysql_query($query1);
    				$row1 = mysql_fetch_row($result1);
    				$query2 = "SELECT paragraph2 FROM group12db.review WHERE r_id = ".$j.";";
    				$result2 = mysql_query($query2);
    				$row2 = mysql_fetch_row($result2);
    				
    
    
    				//displace  restaurant in the catagory
    				echo $row[0].'<br />';
    				//creates a button for all the restaurant and sends the parameters to the javascript to displace paragraph one and two
    			  echo '<button onclick="american($row1[0],$row2[0])">click</button>';
    			  // echo '<input type="button" onclick="alert(\.$row1[0].)"/>';		
    			}
    			?>
    Code:
    <script>
    
    function american(infoo,infoos)
    {
    	//alert("Welcome " + title);
    document.getElementById("mainInfo").innerHTML=infoo;
    document.getElementById("title").innerHTML=infoos;
    
    }
    </script>
    '<button onclick="american($row1[0],$row2[0])">click</button>';
    You've got your echo wrapped in single quotes. Anything in single quotes will be echoed/printed exactly as it is. Even though you've got the actual function inside of double quotes, the quotes themselves are still within the single quotes. Change the single quotes to double quotes and you should be fine.

    I only glanced over your code, but that seems to be the problem you're having.
    Last edited by elitis; 05-12-2013 at 06:00 AM.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    40
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hey thxs for the help, but when I change it to double quote it gives my syntax error
    Code:
    			  echo "<button onclick="american($row1[0],$row2[0])">click</button>";
    but when I change it to
    Code:
      echo "<button onclick='american($row1[0],$row2[0])'>click</button>";
    single quote for the function no error, but still does not work
    I also tried just
    Code:
    echo "<button onclick='american('lol')'>click</button>";
    thinking maybe its the varables row1 row2, but the title does not change to lol. Is something with the java script?
    Code:
    function american(tester,lester)
    {
    	
    document.getElementById("mainInfo").innerHTML=lester;
    document.getElementById("title").innerHTML=tester;
    
    }
    I just have no idea spent so much time trying to figure out why its not working.

  • #4
    New Coder
    Join Date
    May 2013
    Posts
    40
    Thanks
    14
    Thanked 0 Times in 0 Posts
    hey after messing around the code I got it to work somewhat.
    I changed
    Code:
    echo "<button onclick='american('lol')'>click</button>";
    to
    Code:
    echo "<button onclick=american('lol')>click</button>";
    and now it seems to change title to lol, but when I try to put a variable instead of the lol it does not change anything. Is something wrong with the variable? I did echo and it displays the content of the variable so I don't get why its not working.

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    this works:

    PHP Code:

    $row1
    [0]= 'Row 1';
    $row2[0]= 'Row 2';

    echo 
    "
    <title id='title'>test</title>
    <script type=\"text/javascript\">

    function american(infoo,infoos)
    {
        //alert(\"Welcome \" + title);

    document.getElementById(\"title\").innerHTML=infoos;

    }
    </script>"
    ;

    echo  
    "<button onclick=\"american('$row1[0]','$row2[0]')\">click</button>"
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #6
    New Coder
    Join Date
    May 2013
    Posts
    40
    Thanks
    14
    Thanked 0 Times in 0 Posts
    thxs for the help i think i figured it out


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •