Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,152,366 members, 7,815,783 topics. Date: Thursday, 02 May 2024 at 06:16 PM |
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)
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! *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" // 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: /* 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: 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: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: 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: It didn't work |
Re: See The Javascript Code That Is Turning My Brain by sleepingdemon: 3:55pm On Jun 26, 2016 |
lillylove2: 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: 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: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. 1 Like |
Re: See The Javascript Code That Is Turning My Brain by wisemania(m): 5:58pm On Jun 26, 2016 |
Booyakasha: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: 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: 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: Try this
|
Re: See The Javascript Code That Is Turning My Brain by hollyfat(m): 9:33pm On Jun 26, 2016 |
hollyfat: A better option
|
Re: See The Javascript Code That Is Turning My Brain by Kodejuice: 9:33pm On Jun 26, 2016 |
lillylove2: its quite easy with jQuery, but Vanilla JS it is.
|
Re: See The Javascript Code That Is Turning My Brain by teampregar(m): 3:25pm On Jun 27, 2016 |
lillylove2: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:You could have checked stackoverflow its there.. try this: HTML <a href="#" onclick="myId(this.id)" id="//the php stuffs">Click Me</a> JAVASCRIPT
1 Like |
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 |