How concatenate a PHP variable into a HTML link? - javascript

i need to concatenate this variable $idCantiere into a href attribute
<a class="btn btn-primary" href="../../../paginaIniziale/sceltaModificaAffidatario.php?idCantiere='.$idCantiere.'">Continua -></a> `
In other words, I need that when I press the "Continua->" button, I will be redirected to the HREF page but with the addition, at the end of the link, of the "idCantiere"

Try the following:
<a class="btn btn-primary" href="../../../paginaIniziale/sceltaModificaAffidatario.php?idCantiere=<?php echo $idCantiere ?>Continua -></a>
What this does is the following:
opens <?php tags so we can write php.
echo your variable in the html template
Close the php by ?>
When you use echo in php it will output the value of a variable to the actual HTML file that will be produced. Thus you use echo when you need to output something dynamic into the html.

Here are two possible solutions:
https://www.tehplayground.com/rpPcUnram5TAxdAs
Have fun!
edit:
sorry for that here is the code:
No 1:
<?php
$idCantiere = 123;
print '<a class="btn btn-primary" href="../../../paginaIniziale/sceltaModificaAffidatario.php?idCantiere='.$idCantiere.'">Continua -></a>';
?>
No 2:
<?php
$idCantiere = 123;
?>
....
<a class="btn btn-primary" href="../../../paginaIniziale/sceltaModificaAffidatario.php?idCantiere=<?php print $idCantiere; ?>">Continua -></a>

Related

How to change a href when using multiple variables in PHP?

So I didn't found what I was looking for in last two days.
To be clear I have a table "tracks" in my database.
So I can show from "tracks" the "demo" field wich is an audio file "url"..
<?= $tracks['demo']; ?>
I have multiple url's but then I need to replace the a href
<a href="<?php echo $tracks['demo'];?>"
in a logical way.
In simple terms it's like I want to click on button 1 that loads
url 1 into this a href with the ID, title field from that track.
When you press button 2 you need to load another url and replace url 1 with url2.
I tried many ways including javascript but it won't work.
Problem now is that when I list 4 items it always loads the latest "URL" i have posted in my source code. :)
When I echo out <?php echo $tracks['demo]; ?> on all items I can see the correct url's so the functionality to replace the url is my issue.
* I basically want to load many mp3 url's in a player I made in the footer of my website. It works by hardcoding the urls in the url field but this is not what it should be... *
Code:
<?php while($tracks = mysqli_fetch_assoc($muziek)) : ?>
<div class="col-md-2 viny" id="muziek">
<h4><?= $tracks['title']; ?></h4>
<img src="<?= $tracks['img']; ?>"
alt=" <?= $tracks['title']; ?> />
<p class="artist">Artist: <?= $tracks['artist']; ?></p>
</div>
<?= $tracks['demo'] = $audiourl ; ?>
<button type="button" onclick="play">Play</button>
</div>
<?php endwhile; ?>
*THIS LOOPS PERFECT FOR ALL ITEMS TRACKS ARE LISTED PERFECT *
In my player I have
<a href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a>
function play(){
I'm not good at JS... And this is my issue because it now loads the latest output from the php loop...
}
I don't know how you are replacing it but
Why not pass the audio in the function. Here like this
<button type="button" onclick="play(<?= $tracks['demo'] = $audiourl ; ?>)">Play</button>
assign a id to anchor tag
<a id="someId" href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a>
and in the play function, you receive it like this
function play(audioUrl){
}
and change the href like this in the play function
If you are using jquery
$('#someId').attr("href", audioUrl);
sometimes the above does not works. Can't remember why but if that does not works try this
$('#someId').href = audioUrl
If you are using javascript
document.getElementById('abcd').href = audioUrl
So I tried many things but I had many issues with the player it's js file using the same id's and so on.
The new logic I tried out seems to work:
<form action="" method="POST">
<input type="submit" value="Play" name="Play">
</form>
<?php
if (isset($_POST["Play"]))
{
echo $tracks['demo'];
}else{
echo ' ';
}
?>
There is more going on but I tried to write it down as simple as this to show the logic. All Track url's are hidden and when the page loads there is an empty $audiourl by default loaded in the a href from my audioplayer. By clicking the button play I show the url in the source but not on the site.
Then the latest $audiourl variable changes to this value.

How to send php variable onclick of button to javascript function as parameter

I am using codeigniter 3. Below is the code of the view page, where data inside the $row->poll_question; is coming from the database. Thus, the different values coming inside this variable are echoed on a page called voting.php. And just beside this variable, there is a button called View Poll.
Now, on clicking this button I want to pass the data of this php variable to javascript section. And simply alert and check in javascript. Please help. I have pasted the code I tried.
voting.php
<h3 class="val" id="val" style="color:rgb(158, 158, 158);font-family:
arial;margin-top: 2%;" ><?php echo $row->poll_question; ?></h3>
</td>
<td>
<a class="btn btn-lg btn-primary view_button" id="view_button"
style="margin-left: 18%;padding:10%;border-radius: 10px;"
onclick="fetch_val(<?php echo $row->poll_question ?>)">View Poll</a>
Javascript
function fetch_val(val)
{
alert(val);
}
You have forgot to add quote on '<?php echo $row->poll_question ?>'. Check updated code below
<h3 class="val" id="val" style="color:rgb(158, 158, 158);font-family:
arial;margin-top: 2%;" ><?php echo $row->poll_question; ?></h3>
</td>
<td>
<a class="btn btn-lg btn-primary view_button" id="view_button"
style="margin-left: 18%;padding:10%;border-radius: 10px;"
onclick="fetch_val('<?php echo $row->poll_question ?>')">View Poll</a>
I don't recommend the redundant storage of the string.
You already have the value in your id'ed <h3> tag. Just use that.
Further advice move all stylings to an external stylesheet and use a javascript listener on your #view_button.
You could stay with an inline function call:
<h3 class="val" id="val"><?php echo $row->poll_question; ?></h3>
<a class="btn btn-lg btn-primary view_button" id="view_button"
onclick="fetch_val(document.getElementById('val').innerHTML);">View Poll</a>
Or trigger fetch_val() via a listener written in your javascript (this is the cleaner / more professional way):
document.getElementById('view_button').onclick = function() {
alert(document.getElementById('val').innerHTML);
}
About writing a pure javascript onclick handler: https://stackoverflow.com/a/17633164/2943403
Assigning a custom function to an event:
https://stackoverflow.com/a/24665785/2943403
Using either of my above techniques will remove the possibility of quoting issues caused by questions like What is your mother's maiden name?.

How do I use PHP to encode a string containing quotes to make it safe for inline 'onclick'?

I have a variable in a database that could potentially contain single or double quotes. When I retrieve the variable from the database, it is written with PHP into an inline "onclick" hander:
echo '<li><a onClick="a4e.duplicate_assignment('.$this_assignment['id'].',\''.htmlspecialchars($this_assignment['title'],ENT_QUOTES).'\',\'/assignments/'.$type.'/\');" href="javascript:void(0);">';echo '<i class="fa fa-copy"></i> Duplicate assignment</a></li>';
This produces HTML that looks like this in the page source:
<li><a onClick="a4e.duplicate_assignment(92,'ELLLO - 'If I had a million dollars'','/assignments/cloze/');" href="javascript:void(0);"><i class="fa fa-copy"></i> Duplicate assignment</a></li>
However, clicking the link produces the following error in the console:
Uncaught SyntaxError: missing ) after argument list
I thought using the PHP function "htmlspecialchars" would mitigate this issue, but it doesn't seem to work.
Any help greatly appreciated.
P.S. It is not possible in this case to use a Javascript "onclick" handler - it has to be inline HTML. Also, it is not possible to ban the use of quotation marks in the variable value.
Try using the function:
addslashes()
EDIT: This method will take care of the quotes in the string itself, but may not be suitable if you need to retain quotes for HTML insertion. Read the docs carefully.
http://php.net/manual/en/function.addslashes.php
its because your Unicode (') is also being treated as '
use this
<li><a onClick="a4e.duplicate_assignment(92,'ELLLO - \'If I had a million dollars\'','/assignments/cloze');" href="javascript:void(0);"><i class="fa fa-copy"></i> Duplicate assignment</a></li>
Use json_encode to convert a value to a JavaScript literal (with all necessary escaping).
Use htmlspecialchars to convert a value (such as a JavaScript program) to something safe to place in an HTML attribute value.
$id = $this_assignment['id'];
$title = $this_assignment['title'];
$url = "/assignments/$type/";
$js_id = json_encode($id);
$js_title = json_encode($title);
$js_url = json_encode($url);
$js = "a4e.duplicate_assignment($js_id, $js_title, $js_url);
$html_js = htmlspecialchars($js, ENT_QUOTES);
?>
<li>
<a href="javascript:void(0);" onclick="<?php echo $html_js; ?>">
<i class="fa fa-copy"></i>
Duplicate assignment
</a>
<li>
A better approach would be to use progressive enhancement and non-inline JS. You've ruled that out, but you should try to remove that restriction.
$id = $this_assignment['id'];
$title = $this_assignment['title'];
$url = "/assignments/$type/";
$html_id = htmlspecialchars($id, ENT_QUOTES);
$html_title = htmlspecialchars($title, ENT_QUOTES);
$html_url = htmlspecialchars($url, ENT_QUOTES);
?>
<li>
<a href="<?php echo $html_url; ?>" data-title="<?php echo $html_title; ?>" data-id="<?php echo $html_id; ?>">
<i class="fa fa-copy"></i>
Duplicate assignment
</a>
<li>
<!-- and later -->
<script>
document.querySelector("a").addEventListener("click", duplicate_assignment_handler);
function duplicate_assignment_handler(e) {
e.preventDefault();
a4e.duplicate_assignment(this.dataset.id, this.dataset.title, this.href);
}
</script>
</script>

Including php script inside javascript confirm box

i have included a confirm box in my PHP page which works fine the following is the code
<a href="" onclick="return confirm('Acc No : \nCANDIDATE NAME : \n\nPLEASE CONFIRM ')" >hi</a>
now i want to display value returned by PHP after Acc NO and CANDIDATE NAME i have written a code which is not working the code is below
<a href="" onclick="return confirm('AIN NO :<?=fetchresullt['acc_no']; ?> \nCANDIDATE NAME : <?=fetchresullt['acc_candidatename']; ?> \n\nPLEASE CONFIRM ')" >hi</a>
please help me out
Try with this
<a href="" onclick="return confirm('AIN NO : "<?php echo fetchresullt['acc_no']; ?>" \nCANDIDATE NAME : "<?php echo fetchresullt['acc_candidatename']; ?>" \n\nPLEASE CONFIRM ')" >hi</a>
Are you sure fetchresullt['acc_no'] is spelt correctly? Doesn't it need to be a variable? ie:
<?=$fetchresult['acc_no']?>
Also make sure you are not including it in a .js file, but make it inline JS code in a .php file.

javascript onClick is not not working in php

I am echoing a html form with php which has the following lines:
<a class='btn btn-sm btn-danger pull-right' onClick=return confirm('Delete This account?'); href='".url('deleteuser/'.$order->id)."' >Delete</a>
This correctly goes to the url specified. However, the onClick method is never triggered. Any help is much appreciated.
EDIT: Clarified pure HTML versus echo string.
Put quotes around the onclick attribute value.
i.e. in pure HTML:
onClick="return confirm('Delete This account?');"
So if you are outputting this via echo, you need to escape the respective type of quote characters with a backslash.
echo 'onClick="return confirm(\'Delete This account?\');"';
or
echo "onClick=\"return confirm('Delete This account?');\"";
Upvoted #faintsignal answer because it is correct, your confusion of single quotes is causing, however a much better way to do this is separate the PHP from the HTML:
<?php
foreach($someItem as $someKey => $order){
?>
<a class="btn btn-sm btn-danger pull-right" onClick="return confirm('Delete This account?');" href="<?php echo url('deleteuser/'.$order->id); ?>">Delete</a>
<?php
}
?>
Just providing an alternative, and it's generally considered good practice because you have clear separation of duties and its easier to maintain.
EDIT: provided a super generic example of using foreach, obviously you can mod for your setup but it shows you can separate the function and it will run just fine.

Categories