PHP jQuery value problem - php

I have problem to send value from php to jQuery script.
PHP looks like that:
echo "<a id='klik' value='".$row['id']."' onclick='help()' href='http://www.something.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
and script jQuery:
function help(){
var j = jQuery.noConflict();
var zmienna = j('#klik').val();
alert(zmienna);
j.post('licznik.php',{id:zmienna}, function(data) {
alert(data);
});
}
licznik.php
$p=$_POST;
$id=$p['id'];
echo $id;
$wtf = "UPDATE tag_content SET wyswietlenia=wyswietlenia+1 WHERE id='$id'";
$result = mysql_query($wtf);
And as I tested, it has problem at the begining (alert(zmienna); doesn't work, shows nothing). How to fix it?
Thx for help and if u want more informations (like more code etc.) let me know.

{id:zmienna} is not JSON, {'id':'zmienna'} is. Fix that.

The a tag can't have a value. What you can do is pass the id as a parameter:
echo '<a id="klik" onclick="help(\''.$row['id'].'\')">...</a>';
and in Javascript:
function help(zmienna) {
alert(zmienna);
}

That's probably because the anchor tag has no value attribute (http://www.w3.org/TR/html4/struct/links.html). Try this instead:
var zmienna = j('#klik').attr('value');
I would also advice against using value. If I need additional data, I use a tag like data.

Related

Change HREF link depending on elements "value" in jquery

I have the following PHP that returns records from a my MYSQL table. These records are displayed as LINKS. See code below...
<div class="slide1" id="u1026">
<?php while ($row = mysql_fetch_array($query_rental)) {
echo "<a class='fancybox fancybox.iframe' id='rental' value={$row['layout']} href=\"brochures\items-rental.php?id={$row['client_name']}\"></a>";
}?>
</div>
What I would like, is for the HREF link to change to
\"brochures\items-rental-layout2.php?id={$row['client_name']}\
If VALUE contains the text "layout2". I know that I can change HREF using jquery code
$(document).ready(function () {
$("#event").attr("href", "http://the.new.url")
});
I'm just not sure how to do that depending if the VALUE contains text "layout2". Any help is much appreciated. Thanks
You can just do it straight in the PHP code:
while ($row = mysql_fetch_array($query_rental)) {
$layoutFlag = $row['layout'] == 'layout2' ? '-layout2' : '';
echo "<a class='fancybox fancybox.iframe' id='rental' value=\"{$row['layout']}\" href=\"brochures\items-rental{$layoutFlag}.php?id={$row['client_name']}\"></a>";
}
You could also do it with Javascript:
$(function () {
// I'm assuming you are going to turn it into a rental class, otherwise change the selector to whatever.
$("a.rental").each(function() {
var rentalItem = $(this);
if (rentalItem.attr('value') === 'layout2') {
// You can choose what to replace, as long as you know it will replace EXACTLY what you want it to. I'm just going with Regex's ^ (start-of-line) operator to make sure that what we are replacing is at the start of the line...
rentalItem.attr('href', rentalItem.attr('href').replace(/^brochures\\items\-rental/, 'brochures\\items-rental-layout2'));
});
});
As you can see, just doing it in PHP is so much easier.
Also as a side note, you are creating multiple elements with the same id. Maybe you meant class='fancybox fancybox.iframe rental'?
And as a second side note, I suggest using the data- prefix for holding custom data. In layout's case, use data-layout='layout-whatever'. You can then use .attr('data-layout') to get the layout attribute (it's easier to understand what that code is doing too!).
You can either run the IF statement on the PHP loop
while ($row = mysql_fetch_array($query_rental)) {
echo "<a class='fancybox fancybox.iframe' id='rental' value={$row['layout']} href=\"brochures\items-rental".($row['layout'] == 'layout2' ? '-layout2' : '').".php?id={$row['client_name']}\"></a>";
}
Or by jQuery
$( "a.fancybox" ).each(function( index ) {
if($(this).val() == "layout2") {
oldHref = $(this).attr('href');
newHref = oldHref.replace('items-rental.php', 'items-rental-layout2.php')
$(this).attr('href', newHref);
}
});
all your links have the same ID which can cause some issues when you wuold want to work with them with jQuery.
If you have more a tags with the fancybox class, try adding a unique class to these tags and update the each loop

HTML, PHP, JavaScript -- How to make a button for each individual record in a database? Seems simple but doesn't work

I have an SQL database (I know for sure it includes remote access, if that's necessary to access a database through php). It definitely has at least one record in it, and I need to get each record and create a corresponding button that links to a php file, taking the first field for that record as a/n argument, variable, parameter, or whatever you call the whatever.php?variable=value.
For some reason, this code just gives me a blank page. Why?
<?php
$connection=mysqli_connect("myhost","myusername","mypassword","mydatabase");
$result=mysqli_query($connection, "SELECT * FROM myTable");
$resultArray=array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultArray, $row['ID']);
}
$resultArrayImplode = implode(",", $resultArray);
?>
<script>
var resultArray = <?php echo $resultArrayImplode; ?>
arrayEntries = new Array();
arrayEntries = resultArray.split(",");
function CreateButtons(element, index, array) {
var button = document.createElement("input");
button.type = "button";
button.value = element;
button.onclick = function() {
document.location.href = "ButtonClicked.php?id=" + element;
}
document.body.appendChild(button);
}
arrayEntries.forEach(CreateButtons);
</script>
Thanks!
Your javascript assignment to resultArray is probably not syntactically correct due to quote characters, etc. Luckily, PHP's JSON functions automagically create good javascript for you.
Try this for the javascript output:
var arrayEntries = <?php echo json_encode($resultArray)?>;
Your problem is $resultArrayImplode; is a string, so
var resultArray = <?php echo $resultArrayImplode; ?>
renders as:
var resultArray = 1,2,3,4,5
Which is a syntax error.
What you can do is use JSON. JSON syntax is JavaScript syntax, so all you need to do is:
var arrayEntries = <?php echo json_encode($resultArray); ?>;
This should render as something like:
var arrayEntries = [1,2,3,4,5];
Which is perfect JavaScript syntax. Then the rest of your code should work.
I just don't see why do you need to use javascript for this, cant you just do the following :
<?PHP
foreach($resultArray as $result)
{
?>
Im a button click me</div>
<?PHP
}
?>
in my opinion, i see no real need for you to use javascript for this.

Targeting text value from link for multiple links in one output

I can't find/think of a way to solve this problem.
I have a list of links outputted by a php script and I need to get the text value from the item (link) that's been clicked.
Here is the php part that outputs data:
$query = mysql_query("SELECT NAME FROM ccm WHERE NAME LIKE '$value%'");
while( $run = mysql_fetch_array($query)){
$name = $run['NAME'];
echo '<a id="rez_link" onClick="klik();">'.$name.'</a>';
}
And here are some of my attempts to fetch the .text() from the link that's been clicked:
var value = $('a#rez_link').text(); //this one targets every text if there are multiple search result from the query
var value = jQuery(this).find("a").text(); //this one returns nothing
So how do I do it?
Maybe I should modify the php script so that it outputs new links with id=""+i and then target them like that in jQuery or something like that.
Is there a simple way to do this?
You can't have more anchor tags with same id
echo '<a id="rez_link" onClick="klik();">'.$name.'</a>';
it's better to use class instead
echo '<a class="rez_link">'.$name.'</a>';
$(function() {
$(".rez_link").on("click",function()
{
var text = $(this).text();
alert(text);
}
});
$(function() {
$('.rez_link').bind('click', function(e) {
e.preventDefault();
var linkText = $(this).text();
$('#show').text(linkText);
});
});
It's this what you try to do ?

adding jquery a href class selector after PHP entry has been called to DOM dynamically

I've been trying to come up with a solution for this problem. I thought .live() would help but it isn't used in jquery 1.9.1
I have this function - the gist of which is this:
while($row = $result->fetch_assoc()) {
$currentID = $row['UserID'];
$query2="SELECT FirstName, LastName, UserID FROM Users WHERE UserID = $currentID";
$result2 = $mysqli->query($query2);
$row1 = $result2->fetch_assoc();
echo '<div class="commentEntry">';
echo "<h5><a href='user.php?User=".$row1['UserID']."'>".$row1['FirstName']." ".$row1['LastName']."</a></h5>";
echo "<p>".$row['Content']."</p>";
echo "<p class='agree'>Agree</p>";
echo "</div>";
}
This function works, it isn't the issue.
So essentially with jquery I want to get hold of all dynamically added instances of a.agreeWith.
To test, I am using this:
$(document).ready(function() {
$('a.agreeWith').click(function(event) {
event.preventDefault();
alert("test");
var agree = $(this).id;
var data = 'Agree=' + agree.val();
});
});
Now what I find isn't happening, is that the alert never appears. The a href link still triggers and so clearly there is no call.
I have tested it simply by writing whatever into the page, and it works, the alert appears.
So clearly there is an issue with dynamically loaded information and trying to apply jquery to it.
The question is. How do I get around this?
Thanks for your help.
This should work for you at least to get your alert working. Not sure what your trying to do after....
$(document).ready(function() {
$(document.body).on('click', 'a.agreeWith', function(event) {
event.preventDefault();
alert("test");
var agree = $(this).id;
var data = 'Agree=' + agree.val();
});
});

var attribute echoed with php and not submitted from form

Using this
`$(function(){$(".signaler").click(function(){var element=$(this);var I = element.attr("id");var page = $('#page').attr('value');var info = "id="+I+"& page="+ page;$("#signaler"+I).hide();$("#load"+I).html('<img class="think" src="load.gif" >');$.ajax({type:"POST",url:"signaler.php",data:info,success:function(){$("#load"+I).empty();$("#ok"+I).fadeIn(200).show();}});return false;});});`
but have a form with hidden inputs only so i can echo current user info into it, was wondering if I could put it straight into jquery? Working fine though.
With a form you would use the following
var page=$('#page').attr('value');
I would like to set the id with php echoing user name/id..
Something like var id=$('<?php echo $user/$id... ?>');
If someone could point out the right jquery syntax, would be very greatful!
var id = $(<?= $_POST["whatever_it_was"] ?>)
pretty much what you used as an example...
<?php echo $user . '/' . $id ?>
As long as the file in question is being parsed as php by the server that is...
Firstly you should get the value of a form input using the val() method:
$('input#id').val();
Then on the PHP side if you POST to the page using AJAX then do:
'var id=$('<?php echo $_POST['user'] . '/' . $_POST['id'] ?>')';
What you have is essentially a string you wish to use for somewhere in you jQuery. Well in that case what you have is essentially correct. If in your PHP file you do (notice the quotation marks:
var id = "<?php echo $yourId ?>";
You can then use that variable (which holds your PHP string) normally. The following would for example select the element with the id in the variable id:
$('#' + id)

Categories