I am currently trying to pass data from my server to my main page. I currently have my php echoing for each returned result:
while($row = mysql_fetch_array($result))
{
echo "<img class='s' id=" . $row['id'] . " src=QR/" . $row['src'] ".png>";
}
when i tried adding a field, num, after id:
<img class='s' id=(rowid) num=".$row['num']." .......
I get undefined when i use
alert(this.num)
but
alert(this.id)
works. How can I pass the num value too?
EDIT:
Hey Everyone, thanks for you help, I have included a jsfiddle on how i solved this problem.
http://jsfiddle.net/3yzcx/4/
I had to use jQuery and used .attr() to define my own attribute called num and called that out.
Very simple. id is a valid property for the img tag. num is not. You can't assign arbitrary properties to a tag and expect it to work.
If you want to pass your PHP data to javascript, using a hidden form element or even more simply something like this:
<?php
echo '<script type="text/javascript">';
echo 'var num = ' . $num . ';';
echo '</script>';
?>
Of course I know you're in a loop, this is just an example. You could easily make it an array or something.
Adding properties doesn't work. You'll want to add an <input type=hidden value='num'> or something inside the <div> where num is your number data. You'll need to make the input field addressable by giving it an ID as well:
echo("<input type=hidden id=\"$row[id]-num\" value='num'>");
Also, I see a closing div tag but no opening tag. You'll want to fix that!
Technically you are not supposed to assign custom attributes. Jcolebrand's original post is worth an upvote due to the discussion about the data prefix. However, even though it is technically not correct, you should be able to get the value with the getAttribute() function.
this.getAttribute("num");
That will work in Firefox, but no guarantees in other browsers.
I've not tried this outside of jQuery, but within that framework you would use
alert(this.attr("num"));
to retrieve non-standard attributes. May work with plain javascript as well, but I've not tried it.
Related
I have a page called order_page.php and it has a hyperlink that should pass the Order ID of a particular order to another page called edit.php.
I looked up some tutorials on youtube but it's still not passing what I want.
This is what I've tried.
echo "<td class='total'></td><td class='total' id='total2'>TOTAL:</td><td class='total' id='total2'>".$float_total."</td><td> <a href='edit.php'?edit=$row[orderid]>Edit</a></td>";
And then this is the edit.php code
<?php
if(isset($_GET['edit']))
{
$oid = $_GET['edit'];
$res = mysql_query("SELECT * FROM orderdb");
$rowww= mysql_fetch_array($res);
}
?>
It doesn't seem to pass an orderid. If you want I can show the full source code.
Your problem appears to have arisen from losing track of all those quotation marks. Consider not using echo() for the whole line because it doesn't seem to be serving any advantage in your case. In this way you can then use ascsoftw's answer.
In addition, take a look at your href=. The part containing your query string is outside of the single quotes when it should be inside.
Given these mistakes it is also highly advisable that you don't immediately get into the db side of things without first testing what your GET returns at the destination script.
First you need to check if your URL has id or not so you get idea where things goes wrong.
You need to change following line
echo "<td class='total'></td><td class='total' id='total2'>TOTAL:</td><td class='total' id='total2'>".$float_total."</td><td> <a href='edit.php'?edit=$row[orderid]>Edit</a></td>";
By
echo "<td class='total'></td><td class='total' id='total2'>TOTAL:</td><td class='total' id='total2'>".$float_total."</td><td> <a href='edit.php?edit='".$row[orderid]."'>Edit</a></td>";
I want to use jquery so i can add dynamically the "store_name" data of the code bellow inside the page title attribute for seo purposes.
<?php
$store_info=array(
'store_id'=>$store_id ,
'phone'=>$data['phone'],
'store_name'=>$data['store_name'],
);
?>
I already tried to use this code:
<script src="http://code.jquery.com/jquery-1.11.0.min.js">
$(function(){
$(document).attr('title', $data['store_name']);
});
</script>
but didn't work at all. What in the world i'm doing wrong ?
People mentioned that already there is a similar question here but this isn't what i 'm looking for. In my question i want to pass in the title attribute, data from a php array as i described above.
It took me a moment to figure this out, and then it hit me:
Remove the src="http://code.jquery.com/jquery-1.11.0.min.js" inside your script tag, since that's ignoring everything inside the tag itself. You'll then need two script tags:
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<?php
$store_info['store_name'] = 'new_title';
echo '<script type="text/javascript">'.
'$(function(){$(document).attr("title", "' .
$store_info['store_name'] .
'" );});</script>'
?>
From the w3.org docs:
If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.
I have an input box in index.php which takes some user input and queries a database using that input field as an attribute. This input is stored in $arrInput[0].
I also have a link to inspect.php which, when clicked, takes me to inspect.php but I need to pass $resultArr[0] to inspect.php so that I can use that variable to continue doing other things within inspect.php. How can I achieve this? \
Currently this is what I have:
/* inspect.php */
// Create a table with some rows. Then create this
// column which has the inspect link
echo "<td> <a href='inspect.php?id=<?php echo $resultArr[0]; ?>'>Inspect</a>
</td>";
...
But I don't know how to access $resultArr[0] within inspect.php. I was told $_GET[] would help, but I'm not sure how and googling the subject hasn't been much help.
Can anyone please help?
Thank you
echo "<td><a href='inspect.php?id=".$resultArr[0]."'>Inspect</a></td>";
I think you are using wrong syntax. Please check above code. You can the access 'id' key inside $_GET.
Not that hard. use $_GET['id'].
I am using codeigniter.I have simple form_dropdown(). I want to execute an onchange() function for my drop down.
I will get the string which was selected through JavaScript.
This is my code but its not working
$js ="onChange=message()";
echo "<script type=\"text/javascript\" > function message(){ var no=document.getElementById('name').value;
alert(sr);
}
</script>";
echo form_dropdown('name',$data,'large',$js);
form_dropdown doesn't automatically add an id to the element, so getElementById won't work as written.
also, for understanding's sake, the guide doesn't do a good job of explaining the fourth parameter. Calling it $js is misleading, as it well add anything you give it to the attributes list.
Change the one line of code to this (note the quotes around the onChange function as well):
$js = 'id = "name" onChange="message();"';
make sure you're viewing your source code after it's output to verify it is outputting valid html as well.
Also, on Chrome, F12 will bring up your developer tools, and hitting the esc key will toggle showing your JavaScript console, which will hopefully give you meaningful errors if everything doesn't go as planned.
Edit:
It also looks like you're setting the value of a variable called no, but alerting a variable called sr, which doesn't seem to be defined.
you can also simply pass ref from function call that would easy instead of getting value from document.getElementById(), it will work when you will add id in select dropdowm
try this code
<?php
echo "<script type=\"text/javascript\" >
function message(element){
var no=element.value;
alert(no);
}
</script>";
echo form_dropdown('name',$data,'large', 'onChange="message(this);"');
?>
echo "<td> + manuf + </td>";
Is this above ever going to work??
I'm pulling results from a mysql db to edit the contents but need the jQuery functionality to edit it, hence the embedded javascript variable...
EDIT:
Sorry for the lack of context, its related to another question i've asked on here Mysql edit users orders they have placed
this is the end goal. To edit the order i place, i need to pull the results into an environment similar to how the user placed the order. So my thinking was to include the jQuery functionality to add items to a cart etc, then they could press submit and in the same way i used .Ajax to post the data to an insert php script i would post the values to an update php script! Is this backwards thinking, any advice welcomed!
I suggest you take a look at the follwing.
json_encode
Ajax
JSONP
Now your simplest solution under you circumstances is to do go for the json_encode method. Let me show you an example:
$json_data = array(
'manuf' => $some_munaf_data
);
echo "<script type=\"text/javascript\">";
echo "var Data = " . json_encode(json_data);
echo "</script>";
This will produce an object called Data, and would look like so:
<script type="text/javascript">
var Data = {
munaf : "You value of $some_munaf_data"
}
</script>
Then when you need the data just use Data.munaf and it will hold the value from the PHP Side.
Try just emitting the MySQL content with PHP:
echo "<td id='manuf'>".$manuf."</td>"
Then get the contents with jQuery like this:
var manuf = $('#manuf').text();
Would you not echo out the jQuery within a Javascript code island? You need the client-based code (jQuery) to be able to execute after the server-side code (PHP).
echo '<td><script language = "JavaScript" type = "text/JavaScript">document.write("");</script></td>';
Is this above ever going to work??
Nope. You'd need to output valid JavaScript for the browser to interpret:
echo "<script>document.write('<td>'+manuf+'</td>')</script>";
But that is a dreadful construct, and I can't really see why you would need this, seeing as the td's contents are likely to be static at first.
Consume you have the table echoed with php:
<table id="sometab">
<tr>
<td>
</td>
<tr>
</table>
The jquery for printing resuls in any td is :nth-child(2) takes 2 table td object :
<script type="text/javascript">
$(function(){
$("#sometab tr td:nth-child(2)").html("bla");
})
</script>
Is "manuf" a JS variable or part of a PHP output e.g. part of generated ?
Basically this can easily be done by:
mysql thru PHP(*I can't put table tag..sorry.):
while($row = mysql_fetch_object($result)) {
echo 'tr';
echo 'td a href="#" class="myres"'.$row->manuf.'/a /td';
echo '/tr';
}
then on your JS just attach a "click" handler
$(function() {
$(".myres").click(function() {
//my update handler...
});
});
i think you cant embed the jquery variable in the php like this .
you just give the class name here from here when edit will be click you will get the variable as in submit click in other questions .