Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,366 members, 7,815,783 topics. Date: Thursday, 02 May 2024 at 06:16 PM

See The Javascript Code That Is Turning My Brain - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / See The Javascript Code That Is Turning My Brain (2860 Views)

The Javascript Thread / Help On Html Code That Displays Form Details After Submitting Form / Help Me Keep My Brain In My Skull! Solve This Logic! (2) (3) (4)

(1) (2) (Reply) (Go Down)

See The Javascript Code That Is Turning My Brain by lillylove2(f): 2:42pm On Jun 26, 2016
Please guys help me o. I've searched all over the internet and still can't find solution to this.

I have a link (html element), whose ID is automatically generated with PHP. Whenever the link is clicked, i want to insert the ID of the link into my database. The only problem now is how to get the ID.

This is my mock code:

///// The PHP that generates the id:
<?php
$id=(an array of numbers returned from a while loop);
?>


////the html link
<a href='#' onClick='MyId()' id='<?php echo $id ;?>'>Click Me</a>

/// the javascript function
function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;
}

My question now is how do i get and put the id of the clicked link into variable var ID
Re: See The Javascript Code That Is Turning My Brain by Locotech: 2:51pm On Jun 26, 2016
I'm currently on my laptop learning XML, Java, at the same time developing an App...I may not be able to give you a correct answer but I can guide you in the right direction.


I dont think its wise to equate the Var ID directly to the database field.

I think your should first equate the Var ID to a new variable. And this new variable should be the same with the field name of the ID in the database.

So that when you use the Get command to retrieve it from the database...your Var ID will be equated to its value.

I hope I haven't said nonsense sha! grin


*modified*

OK you are trying to get the generated Id from php and insert it into your data base OK...wait first...let me search it out

This code inserts data from php form to mysql


/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo"wink;

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_POST['firstname']);
$last_name = mysqli_real_escape_string($link, $_POST['lastname']);
$email_address = mysqli_real_escape_string($link, $_POST['email']);

// attempt insert query execution
$sql = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 2:57pm On Jun 26, 2016
^^^ Tnx for the reply. But the problem i have now is totally different from that. My problem is :

How do I get the ID of a link, once i click the link. if I should use onClick='alert.(this.id)', it would alert the ID whenever i click the link. But i want to insert the ID into my database instead of alerting it. Please how do i do that
Re: See The Javascript Code That Is Turning My Brain by Locotech: 3:01pm On Jun 26, 2016
lillylove2:
^^^ Tnx for the reply. But the problem i have now is totally different from that. My problem is :

How do I get the ID of a link, once i click the link. if I should use onClick='alert.(this.id)', it would alert the ID whenever i click the link. But i want to insert the ID into my database instead of alerting it. Please how do i do that

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo";

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_POST['firstname']);
$last_name = mysqli_real_escape_string($link, $_POST['lastname']);
$email_address = mysqli_real_escape_string($link, $_POST['email']);

// attempt insert query execution
$sql = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link)
Re: See The Javascript Code That Is Turning My Brain by dueal(m): 3:01pm On Jun 26, 2016
Understand that your source first gets interpreted on the server side. Place Id = v<?php phpcho $id;?>. Before that make sure $id variable in php is not an array and a proper name.
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 3:03pm On Jun 26, 2016
Locotech:


/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo";

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_POST['firstname']);
$last_name = mysqli_real_escape_string($link, $_POST['lastname']);
$email_address = mysqli_real_escape_string($link, $_POST['email']);

// attempt insert query execution
$sql = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link)

Na wa o. Sir this is not what i'm asking. Thanks for taking the time anyway.
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 3:21pm On Jun 26, 2016
dueal:
Understand that your source first gets interpreted on the server side. Place Id = v<?php phpcho $id;?>. Before that make sure $id variable in php is not an array and a proper name.
Thanks. But let me expansiate further. Suppose I have this three links:

<a href='#' id='1' onClick='getID()> Click Me </a>
<a href='#' id='2' onClick='getID()> Click Me </a>
<a href='#' id='3' onClick='getID()> Click Me </a>

Normally, if the links weren't dynamically generated, I could do this to get the id:

function getID {
var MyID=document.getElementById('2').id;
alert.(MyID);
}

This would return 2 which is the id of the second link.

But the problem is that the links are dynamically generated. And they have unique ids.

Please help me out.
Re: See The Javascript Code That Is Turning My Brain by dueal(m): 3:29pm On Jun 26, 2016
How do you generate and assign d id's? On the server with php I suppose?
Re: See The Javascript Code That Is Turning My Brain by ANTONINEUTRON(m): 3:31pm On Jun 26, 2016
Are U D One Generating D Id Or It Is Auto-Generated??
Re: See The Javascript Code That Is Turning My Brain by dueal(m): 3:35pm On Jun 26, 2016
Try onClick="getID(this)" or onClick="alert(this.id)". Also generate ur id's to start with a letter not a number
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 3:38pm On Jun 26, 2016
dueal:
Try onClick="getID(this)" or onClick="alert(this.id)". Also generate ur id's to start with a letter not a number

Tnx. If I do it this way, it would truly alert the id. But I would like to put the id inside a Variable. How do I do that.

Tanx
Re: See The Javascript Code That Is Turning My Brain by dueal(m): 3:42pm On Jun 26, 2016
You can do: function getID(elem){ var localvar = '"' + elem.id + '"'; } you need to concatenate d quotes with the id to form a proper string.
Re: See The Javascript Code That Is Turning My Brain by dueal(m): 3:43pm On Jun 26, 2016
Advice: after learning JavaScript pick a library like jQuery.
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 3:53pm On Jun 26, 2016
dueal:
You can do: function getID(elem){ var localvar = '"' + elem.id + '"'; } you need to concatenate d quotes with the id to form a proper string.

It didn't work
Re: See The Javascript Code That Is Turning My Brain by sleepingdemon: 3:55pm On Jun 26, 2016
lillylove2:


Tnx. If I do it this way, it would truly alert the id. But I would like to put the id inside a Variable. How do I do that.

Tanx

what u hate to do is simple
in your onclick function, you would do onclick=getID(this.id)

them in your script its like this
function grtID(myid){
}
the variable myid automatically holds the value of the id passed in the onclick function

1 Like

Re: See The Javascript Code That Is Turning My Brain by dueal(m): 3:57pm On Jun 26, 2016
Did u put double quotes btw d single quotes? And not 4 single quotes?
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 4:06pm On Jun 26, 2016
dueal:
Did u put double quotes btw d single quotes? And not 4 single quotes?

I put double quotes
Re: See The Javascript Code That Is Turning My Brain by dueal(m): 4:10pm On Jun 26, 2016
Paste what you have here. And hope u have ur javascript in proper tags or linked
Re: See The Javascript Code That Is Turning My Brain by lillylove2(f): 4:16pm On Jun 26, 2016
Tnx. My system is off. till light comes.
Re: See The Javascript Code That Is Turning My Brain by dueal(m): 4:19pm On Jun 26, 2016
The pains on a Nigerian dev. Ok then.
Re: See The Javascript Code That Is Turning My Brain by Nobody: 5:17pm On Jun 26, 2016
Lol passing a php variable to Javascript is fucking easy!

Inside the php file

<script>
Var id=<? Echo $id;?>;
Alert (id) ;
</script >

Php gets parsed first before any other language in a php file. That's why it's called "preprocessed hypertext programming language"

If your js is external, I only can help you with jquery, download the library and link it as you would link any js file.

In your php file

Do this

<link href="" id="<? Echo $id;? >" myattrib="<? Echo $id;? >" class="class">

In your js file

Var id = $(.class).attrib('myattrib');
Alert (id);

What this does is jquery selects the element with the class of 'class' (hope this isn't confusing)

Then checks for the attribute 'myattrib'

Then assigns the value inside myattrib to the variable 'id'

Hope it helps
Re: See The Javascript Code That Is Turning My Brain by wisemania(m): 5:33pm On Jun 26, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.

I have a link (html element), whose ID is automatically generated with PHP. Whenever the link is clicked, i want to insert the ID of the link into my database. The only problem now is how to get the ID.

This is my mock code:

///// The PHP that generates the id:
<?php
$id=(an array of numbers returned from a while loop);
?>


////the html link
<a href='#' onClick='MyId()' id='<?php echo $id ;?>'>Click Me</a>

/// the javascript function
function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;
}

My question now is how do i get and put the id of the clicked link into variable var ID
I wouldn't know whey you'd want to insert an ID into your database using a js function.your plan is to validate a user?.Why not use pure php for that by creating the entire functions with php.later add some cool functionalities with js (not every client has js enabled).
Good to see a lady here

Dazall...
Re: See The Javascript Code That Is Turning My Brain by Booyakasha(f): 5:49pm On Jun 26, 2016
lol.. most of the people giving you answers don't know what they're even doing.. this is not the right place to ask programming questions, Stackoverflow.com offers genuine instant help. grin

1 Like

Re: See The Javascript Code That Is Turning My Brain by wisemania(m): 5:58pm On Jun 26, 2016
Booyakasha:
lol.. most of the people giving you answers don't know what they're even doing.. this is not the right place to ask programming questions, Stackoverflow.com offers genuine instant help. grin
You don enter trouble..oya dhtml , pcguru prey don show
Re: See The Javascript Code That Is Turning My Brain by spikesC(m): 6:34pm On Jun 26, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.

I have a link (html element), whose ID is automatically generated with PHP. Whenever the link is clicked, i want to insert the ID of the link into my database. The only problem now is how to get the ID.

This is my mock code:

///// The PHP that generates the id:
<?php
$id=(an array of numbers returned from a while loop);
?>


////the html link
<a href='#' onClick='MyId()' id='<?php echo $id ;?>'>Click Me</a>

/// the javascript function
function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;
}

My question now is how do i get and put the id of the clicked link into variable var ID

1. Your Javascript code does not know the ID value, which means you also wouldn't know the value as it is auto generated.
2. You cannot target multiple elements by an ID (atleast theoretically)

Solution:
Give your links a unique class and getElementByClassname.
Retrieve the ID attribute's value and pass that to your PHP script

Preferably, you should use data-* attributes to pass data around instead of using the ID attribute.
Re: See The Javascript Code That Is Turning My Brain by Nobody: 7:24pm On Jun 26, 2016
ooPs i thought it was java **runs outta thread**
Re: See The Javascript Code That Is Turning My Brain by splashz(m): 9:08pm On Jun 26, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.

I have a link (html element), whose ID is automatically generated with PHP. Whenever the link is clicked, i want to insert the ID of the link into my database. The only problem now is how to get the ID.

This is my mock code:

///// The PHP that generates the id:
<?php
$id=(an array of numbers returned from a while loop);
?>


////the html link
<a href='#' onClick='MyId()' id='<?php echo $id ;?>'>Click Me</a>

/// the javascript function
function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;
}

My question now is how do i get and put the id of the clicked link into variable var ID




function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;

since u have picked the id here..
run ajax or xmlhttrequest request to php passing the var to php script

}
Re: See The Javascript Code That Is Turning My Brain by hollyfat(m): 9:22pm On Jun 26, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.

I have a link (html element), whose ID is automatically generated with PHP. Whenever the link is clicked, i want to insert the ID of the link into my database. The only problem now is how to get the ID.

This is my mock code:

///// The PHP that generates the id:
<?php
$id=(an array of numbers returned from a while loop);
?>


////the html link
<a href='#' onClick='MyId()' id='<?php echo $id ;?>'>Click Me</a>

/// the javascript function
function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;
}

My question now is how do i get and put the id of the clicked link into variable var ID

Try this


<?php
$id = rand(1,9); //Generate a random number between 1 and 9
?>
<a onclick="check_id(this)" href="#" id="<?php echo $id; ?>">Click Me</a><!--pass the php id to the "a" tag-->
<script type="text/javascript">
function check_id(i){
var ID = i.getAttribute( "id" ); //Get the attribute of the id
console.log(ID);
}
</script>
Re: See The Javascript Code That Is Turning My Brain by hollyfat(m): 9:33pm On Jun 26, 2016
hollyfat:


Try this


<?php
$id = rand(1,9); //Generate a random number between 1 and 9
?>
<a onclick="check_id(this)" href="#" id="<?php echo $id; ?>">Click Me</a><!--pass the php id to the "a" tag-->
<script type="text/javascript">
function check_id(i){
var ID = i.getAttribute( "id" ); //Get the attribute of the id
console.log(ID);
}
</script>

A better option


<!DOCTYPE html>
<html>
<head>
<title>Js</title>
</head>
<body>
<?php
$id = rand(1,9); //Generate a random number between 1 and 9
?>
<a onclick="check_id(this)" href="#" id="<?php echo $id; ?>">Click Me</a><!--pass the php id to the "a" tag-->
<script type="text/javascript">
function check_id(i){
var ID = i.getAttribute("id"wink; //Get the attribute of the id
console.log(ID);

send_form(ID);
}

function send_form(id){
//this will send the form dynamically

//create a form
var form = document.createElement("form"wink;
form.setAttribute('method',"post"wink; //set form method to post
form.setAttribute('action',"submit.php"wink; //set form action to where you want "submit.php"

//create input element
var input = document.createElement("input"wink;
input.type = "text";
input.name = "id";
input.value = id;

//create a submit button
var btn = document.createElement("input"wink;
btn.type = "submit";
btn.value = "Submit";

// add all elements to the form
form.appendChild(input);
form.appendChild(btn);

//add the form to the body
document.getElementsByTagName('body')[0].appendChild(form);

//submit the form
form.submit();
}
</script>
</body>
</html>
Re: See The Javascript Code That Is Turning My Brain by Kodejuice: 9:33pm On Jun 26, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.

I have a link (html element), whose ID is automatically generated with PHP. Whenever the link is clicked, i want to insert the ID of the link into my database. The only problem now is how to get the ID.

This is my mock code:

///// The PHP that generates the id:
<?php
$id=(an array of numbers returned from a while loop);
?>


////the html link
<a href='#' onClick='MyId()' id='<?php echo $id ;?>'>Click Me</a>

/// the javascript function
function MyId() {
var ID=document.getElementById( the id of the element generated by php ).id;
}

My question now is how do i get and put the id of the clicked link into variable var ID

its quite easy with jQuery, but Vanilla JS it is.


//This should work

<a href='#' onClick='getId(this)' id='<?php echo $id ;?>'>Click Me</a>

function getId(Element){
var ID = Element.getAttribute('id');
//id now stored in ID
}

But if the php code is in the same place with the html, then no need to put the 'this', just put '<? echo $id; ?>' in the link elements onClick function arg.
Re: See The Javascript Code That Is Turning My Brain by teampregar(m): 3:25pm On Jun 27, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.
You could have checked stackoverflow its there..
try this:
HTML
<a href="#" onclick="myId(this.id)" id="//the php stuffs">Click Me</a>
JAVASCRIPT
function myId(a){
var ID=document.getElementById(a).id;
}

1 Like

Re: See The Javascript Code That Is Turning My Brain by teampregar(m): 3:28pm On Jun 27, 2016
lillylove2:
Please guys help me o. I've searched all over the internet and still can't find solution to this.
You could have checked stackoverflow its there..
try this:
HTML
<a href="#" onclick="myId(this.id)" id="//the php stuffs">Click Me</a>
JAVASCRIPT

function myId(a){
var ID=document.getElementById(a).id;
}

1 Like

(1) (2) (Reply)

6 Programming Exercises / Who Wants To Attend A Hackathon? / HNG 8.0!!! Lets Connect√√√

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 101
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.