I am trying to pass variables stored in href links to a function. Im able to define the variables from the query results. My problem is passing it to the function once the hyperlink is clicked. This is my code:
<?php
foreach($pdo->query('SELECT * FROM sk_courses ORDER BY courseID') as $row)
{
echo "<a href='#' onclick='hrefClick(".$row['courseID'].");'/>".$row['courseID']."</a><br>";
}
?>
This is the function:
<script>
function hrefClick($course){
$newCourse=$course;
}
</script>
PHP Code :
<?php
foreach($pdo->query('SELECT * FROM sk_courses ORDER BY courseID') as $row)
{
echo "<a href='#' onclick='hrefClick(".$row['courseID'].");'/>".$row['courseID']."</a><br>";
}
?>
Function should be as:
<script>
function hrefClick(course){
// You can't define php variables in java script as $course etc.
var newCourse=course;
alert(newCourse);
}
</script>
We will be using two file here. From file1.php onclicking the link we will send the data via Ajax to file2.php. It is a Jquery based solution.
//file1.php
<?php
foreach($pdo->query('SELECT * FROM sk_courses ORDER BY courseID') as $row){
echo '<a href="javascript:void()" data-href="'.$row['courseID'].'"/>'.$row['courseID'].'</a><br>';
}
?>
<script>
$('a').click(function(){
var hrefData = $(this).attr('data-href');
if (typeof hrefData !== typeof undefined && hrefData !== false) {
alert(hrefData);
//or You can post the data
$.post( "file2.php", { courseid:hrefData} );
}
});
</script>
You can retrieve the result on file2.php
//file2.php
<?php
if(isset($_POST['courseid'])){
$newCourse = $_POST['courseid'];
}
?>
I was able to find a way to get it done? It seems pretty straightforward. This is what I came up with.
<?php
foreach($pdo->query('SELECT * FROM sk_courses ORDER BY courseID') as $row){
echo "<form name='course".$row['courseID']."' action='index.php' method='GET'/>";
echo "<a href='javascript: document.course".$row['courseID'].".submit();'/>".$row['courseID']."</a>";
echo "<input type='hidden' name='courseID' value='".$row['courseID']."'/><br>";
echo "</form>";
}
?>
Related
I would like to link a variable to a link.
The variable looks something like this which I grab from json.
The json variable for $movieSeat is : A1,A2,B3,V4
Below is the code that I've tried out.
The issue that I am currently having is, the link doesn't seem to be working. It seems like something to do with . and +.
I'm also not too sure when to use . and + for the linking.
<?php
echo "<script> function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=" . $movieSeat. "'; } </script>";
for ($i = 0; $i < $jsonCounter; $i++) {
$movieSeat = $jsonValue[$i]->seatID;
echo "<button onclick = movieBTN(".$movieSeat.")> Movie Seat </button>";
}
?>
First of all, if you're echoing the function from PHP, make sure you use JS variables and not PHP's:
echo "<script> function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=' + x; } </script>";
So it will output:
<script>
function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=' + x; }
</script>
Next, you need to wrap the variable passed to that function with ':
echo "<button onclick = movieBTN('".$movieSeat."')> Movie Seat </button>";
So it will output as:
<button onclick = movieBTN('A1')> Movie Seat </button>
I need to make drop down list as a link to different pages. How do I do that using PHP, MySQL and HTML.
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
$sql="select first_name from users";
$result=mysql_query($sql);
echo "<select First_name=''>";
echo "<a href='index.html'>";
while($row=mysql_fetch_array($result)){
echo ":<option value='".$row['first_name']."'>".$row['first_name']."</option>";
}
echo"</a>";
echo"</select>";
?>
You can't use links on the option tag, in order to do that, you need to use javascript.
You can try to do something like this:
echo "<select name=\"First_name\" onchange=\"document.location='?'+this.value\">";
PHP is a server-side script and does not manipulate a page after a user has adjusted it. Like real time. Only javascript and others do that. PHP creates a page with what you want to see but if you need to change something bases on a dropdown use java. Here is a function that can do that. It unhides a div tag that can have your info you need.
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('dropdown');
var divtag1 = document.getElementById('divtag1');
var divtag2 = document.getElementById('divtag2');
eSelect.onchange = function() {
if(eSelect.selectedIndex === 1) {
divtag1.style.display = 'block';
}
if(eSelect.selectedIndex === 2) {
divtag2.style.display = 'block';
}//or if you want it to open a url
if(eSelect.selectedIndex === 3) {
window.open("https://yourwebsite.com", "_NEW");
}
}
}
</script>
echo "<div id=\"divtag1\" style=\"display:none;\">/*your code*/
</div>";
echo "<div id=\"divtag2\" style=\"display:none;\">/*your code*/
</div>";
i'm using php to generate some button that have their Id and the Id of the text they should show as parameters. But for some reason the function call doesn't seem to be working.
while($row=$query->fetch(PDO::FETCH_BOTH))
{
$reviewDoor=$row[0];
$korteTekst=$row[1];
$langeTekst=$row[2];
echo "<article><h4>Review van $reviewDoor</h4>";
echo "<section>$korteTekst</section>";
if (!empty($langeTekst))
{
echo "<section id='LongReview$teller' class='LongReviews'>$langeTekst</section>";
echo "<button id='LeesMeerKnop$teller' class='LeesMeerKnoppen' onclick='readMoreLess(LeesMeerKnop$teller,LongReview$teller)'>Lees meer...</button>";
}
echo "</article>";
$teller++;
}
the file link is working
<script src="script.js" type="text/javascript"></script>
and this is the function I'm trying to call
function readMoreLess(knopId,tekst) {
var knop=getElementById(knopId);
var review=getElementById(tekst);
window.alert("hallo");
if (knop.textContent ==="Lees meer...")
{
knop.textContent="Minder tekst...";
review.style.display="block";
}
else if (knop.textContent ==="Minder tekst...")
{
knop.textContent="Lees meer...";
review.style.display="none";
}
}
Looks like missing quotes on params values in function call:
echo "<button id='LeesMeerKnop$teller' class='LeesMeerKnoppen' onclick='readMoreLess(\"LeesMeerKnop$teller\",\"LongReview$teller\")'>Lees meer...</button>";
Basically. I have a javascript loop that posts to a php script. To append on the response to a DIV in a log. The response has a button inside it. The problem is, when a new response is made and appended, the jquery button function makes all the previous appended buttons larger. (Probably because it's calling the jquery button() function again on the same DIV elements.)
Is there a way to apply the button() jquery ui function to a button when it's loaded? (Instead of applying it to every element with the same name?)
This php is quite similar to the following (but this is shortened.) Just to give you an idea of what the php script does.
<?php
echo "<script type='text/javascript'>
function addButton(x) {
x = this;
$(x).button({
icons: {
primary: 'ui-icon-alert'
}
});
}
</script><div id='chat_wrap' class='message".$id."' hidden='true'>";
if ($query_run = mysql_query($finalchatq)) {
if (mysql_num_rows(mysql_query($finalchatq)) > 0) {
$counter = 0;
$amount = array();
while ($query_rows = mysql_fetch_assoc($query_run)) {
$time1 = $query_rows['time'];
$time2 = substr_replace($time1, " : ", 10, 1);
$time = str_ireplace('.', '/', $time2);
$text = str_ireplace('removem', '', $query_rows['text']);
$id = $query_rows['id'];
if($query_rows['type']=='notice') {
echo "<div selectable='false'><div id='date'><div id='notice'>NOTICE:</div>Sent At: ".$time."</div><div id='Nchat_message'><div id='chat_text'>".$text."</div><img id='charhead' src='camperhead.php?id=".$query_rows['who']."' id='charhead'></img></div>";
echo "<div id='controls'>";
if(userIsA('admin')||userIsA('mod')) {
echo "<input hidden='hidden' name='idtodel' value='".$id."'></input><input type='button' value='DELETE' id='delete_button'></input>";
} else {
}
echo "</div></div></div><br/><br/>";
}
if($query_rows['type']=='normal'){
echo "<div selectable='false'><div id='date'><div id='by'>".getUserFieldById($query_rows['who'], 'camper_name').":</div>Sent At: ".$time."</div><div id='chat_message'><div id='chat_text'>".$text."</div><img id='charhead' src='camperhead.php?id=".$query_rows['who']."' id='charhead'></img>";
echo "<div id='controls'>";
if(userIsA('admn')||userIsA('md')) {
echo "<button id='delete_button' class='dpp2' onclick='delCom(".$id.")'>Delete</button>";
} else {
echo "<button id='report_button' onload='addButton(this)' onclick='reportCom(".$id.")'>REPORT</button>";
}
echo "</div></div></div></div></div><br/><br/>";
}
}
echo "</div>";
}
}
}
?>
I hope I've made my question clear enough, if you have any concerns please post a comment and I'll reply as soon as I can.
Try using the .on() function in jQuery to define it when created http://api.jquery.com/on/
i want to pass my php variable in one javascript function.
i know it seems simple but i don't know where am i missing something?
<?php
while($gg=mysql_fetch_array($lg))
{
?>
<td id="add_td">
<?php
$id = $gg['p_id'];
echo "<a onclick=cnf('Are you sure you want to delete that?',$id)>"; ?>Delete</a>
</td>
<?php
}
?>
and in my javascript function
function cnf(msg,id)
{
cnf = confirm(msg);
if(cnf) {
parent.location.href = 'p_update.php?d=' + id;
}
}
so i need to know on which id that user had clicked so that i will only delete that id from database.
if i try this thing then it showing error on "cnf" function and its saying like "unterminated string literal"?
if $id is not numeric you should write
<?php
while($gg=mysql_fetch_array($lg))
{
?>
<td id="add_td">
<?php
$id = $gg['p_id'];
echo "<a onclick=cnf('Are you sure you want to delete that?','".$id."')>"; ?>Delete</a>
</td>
<?php
}
?>
Use quotes around php variable '$id'.
echo "<a onclick=cnf('Are you sure you want to delete that?','$id')>";
Another example,
$p is some php variable,
echo "<input type=button value='update' onclick=myfunc('$p')>"
Check your syntax
<?php
while($gg=mysql_fetch_array($lg))
{
?>
<td id="add_td">
<?php
$id = $gg['p_id'];
?>
<a onclick="cnf('Are you sure you want to delete that?',<?=$id?>);">Delete</a>
</td>
<?php
}
?>
I would do something like this instead. HREF is mandatory if you want the "hand" pointer
A unique ID is also mandatory on tags and you need to quote the ID if you pass it in the function instead of what I suggest and give the link the id
function cnf(link,id) {
if (confirm("Are you sure you want to delete "+id) {
link.href = "p_update.php?d=" + id;
return true;
}
return false;
}
<?php
while($gg=mysql_fetch_array($lg)) {
$id = $gg['p_id'];
?>
<td id="add_td<?php echo $id; ?>"><a target="_parent" href="#" id="<?php echo $id; ?>" onclick="return cnf(this)">Delete</a></td>
<?php } ?>
foreach($mas as $k=>$v) {
//echo $k.' = '.$v.'<br>';
echo("
<input type=\"button\" id=\"delete_id".$k."\" value=\"Blablabla\" onclick=\"alert('".$v."');\"/>
");
}
If I put there $k (index) it works but if string (value is string), I get an error
unterminated string literal
In HTML tags this works but not as the function argument!
If $id is a string literal, you should put it into quotes when you are passing it as a parameter to cnf() function in <a ... > tag:
echo "<a onclick=cnf('Are you sure you want to delete that?', '$id')>";