8 Replies - 207 Views - Last Post: 14 December 2014 - 05:25 PM

#1 Tachii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-December 14

How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 12:53 PM

So the problem for me is:
How to do so, when i double-click on the needed line in the table, data from that line transfers into the textareas of my form?
Any suggestions?

Html:

<form class="form-horizontal" name="credit" action="handler.php" method="post" >onsubmit="return validate_form ( );">
<textarea name="Bank"></textarea> <textarea name="BIC"></textarea> <button type="button" onclick="NewWindow()">Banks</button>
</form>


Javascript:
function NewWindow()
{
window.open("pagination.php", "_blank", "toolbar=no, scrollbars=no, resizable=no, top=100, left=100, width=600, height=600");
}


pagination.php:

<html>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "payment";
   
$connection = new mysqli($servername, $username, $password, $dbname);
 
 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "SELECT * FROM banks ORDER BY BankName ASC LIMIT $start_from, 20";
$rs_result = mysqli_query ($connection,$sql);
 
?>
 
<link rel="stylesheet" type="text/css" href="table.css" media="screen" />
<script>
document.querySelector('table').addEventListener('click', function(e){alert(e.target.parentNode.id)}, false);
</script>
<table>
<tr><td><b>BankName</b></td><td><b>BIC</b></td></tr>
<?php
$i = 1;
while ($row = mysqli_fetch_assoc($rs_result)) {
?>      
                        <tr id=<?php echo $row["id"]; ?> 1=<?php echo $row['BankName']; ?> 2=<?php echo $row["BIC"]; ?>>
            <td id=<?php echo $row["BankName"]; ?>><?php echo $row["BankName"]; ?></td>
            <td id=<?php echo $row["BIC"]; ?>><?php echo $row["BIC"]; ?></td>
            </tr>
<?php
};
?>
</table>
<?php
$sql = "SELECT COUNT(BankName) FROM banks";
$rs_result = mysqli_query($connection,$sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);
 
for ($i=1; $i<=$total_pages; $i++) {
            echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};
?>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: How to fill input areas in html form using data from mysql?

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,485
  • Joined: 03-December 12

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 01:24 PM

First of all, it is a VERY VERY bad thing to give any user, system, database, ANYTHING root access.

For your problem, think about what you need to do and layout the steps in english.

1. User clicks on data in table.

2. Data that was clicked on moves to textarea.


a1. How would you determine what the user clicked on?
How would you get the value from that element?

a2. How am I going to identify the element that I need the data to transfer to?
Do I want to replace what is in that element or do I want to append it?

Based on that, what code do I need to solve the 1st step?
Was This Post Helpful? 1
  • +
  • -

#3 Tachii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-December 14

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 01:33 PM

View Postastonecipher, on 14 December 2014 - 01:24 PM, said:

a1. How would you determine what the user clicked on?
How would you get the value from that element?



That's the problem. I don't know how to do that. I'm new to JS, that's why i'm asking.
How can i get the data from the line I click(or doubleclick in my case) on? Is there a way in JS to store data from the element you click on, so you can send it somewhere else later? And if the answer is Yes, how do i specify where i want to send that data?

I've got this, but it's not working:
<script type="text/javascript">document.querySelector('table').addEventListener('click', function(e){alert(e.target.parentNode.id)}, false);</script>

This post has been edited by laytonsdad: 18 December 2014 - 07:21 PM
Reason for edit:: code tags

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,485
  • Joined: 03-December 12

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 01:53 PM

Part of that is going to be where on the page do you have it?

Check this page out and see what you can pull from it.

This post has been edited by astonecipher: 14 December 2014 - 03:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 Tachii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-December 14

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 02:45 PM

Thanks for the link.

I also removed unnecessary code, to simplify the question

<table>
<tr><td><b>name1</b></td><td><b>name2</b></td></tr>
 <tr>
<td>Name1</td>
<td>Name2</td>
</tr>
\
</table>


So basically how do i transfer those Name1 and Name2 into the input fields of my form?

This post has been edited by Dormilich: 15 December 2014 - 01:16 AM

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,485
  • Joined: 03-December 12

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 03:23 PM

Check the link again, I added the part of the page to start your review.
Was This Post Helpful? 1
  • +
  • -

#7 Tachii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-December 14

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 04:53 PM

I tried to launch example from there on my pc, and it's not working.
That's what i'm trying to execute:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>new_file2</title>
		<script type="text/javascript">
			function modifyText() {
  var t2 = document.getElementById("t2");
  if (t2.firstChild.nodeValue == "three") {
    t2.firstChild.nodeValue = "two";
  } else {
    t2.firstChild.nodeValue = "three";
  }
}

// add event listener to table
var el = document.getElementById("outside");
el.addEventListener("click", modifyText, false);
		</script>
	</head>
	<body>
		<table id="outside">
    <tr><td id="t1">one</td></tr>
    <tr><td id="t2">two</td></tr>
</table>
	</body>
</html>


It gives me next error:

Uncaught TypeError: Cannot read property 'addEventListener' of nullnew_file2.html:20 (anonymous function)

I'm using chrome

This post has been edited by Dormilich: 15 December 2014 - 01:15 AM

Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,485
  • Joined: 03-December 12

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 05:07 PM

Here is a bit of advice. Whenever you add <script> tags, place them just before the closing body tag </body>. Otherwise, your script may not work do to the element not existing yet.
Was This Post Helpful? 1
  • +
  • -

#9 Tachii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-December 14

Re: How to fill input areas in html form using data from mysql?

Posted 14 December 2014 - 05:25 PM

Thanks, that works

This post has been edited by Dormilich: 15 December 2014 - 01:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1