How to retrieve value from form_dropdown() in Codeigniter - php

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);"');
?>

Related

Set the value of a button to equal the answer of an SQL query

My code has a text input and submit button which on return hides that form and displays a new button, which works. The problem I'm having is setting the value of the button (or innerHTML) to the answer in my query (which will always only be one). I have the following code:
echo '<form><button id="HCP_Btn" name="HCP_Btn" style="display:none"></button></form>';
$HCP_num = $_POST['HCP_num'];
$HCP_Query="SELECT * FROM HomeCareProviders WHERE Number='". $HCP_num."'";
$HCP_result= mysql_query($HCP_Query) or die(mysql_error());
if (mysql_num_rows($HCP_result)==0){
echo 'Sorry there are no Home Care Providers with the number entered.';
}
//HCP_Btn.innerHTML='.$row["name"].';
else {
$row = mysql_fetch_array($HCP_result);
echo '<script type="text/javascript">
HCP_Btn.style.display="";
document.form.HCP_Btn.innerHTML='.$row["name"].';
</script>';
}
You can use this Javascript code
echo '<script type="text/javascript">
document.getElementById("HCP_Btn").style.display="";
document.getElementById("HCP_Btn").innerHTML="'.$row["name"].'";
</script>';
For first change it like this
echo '<script type="text/javascript">
//HCP_Btn.style.display="";
document.form.HCP_btn.innerHTML=\''.$row["name"].'\';
</script>';
for second check if $row["name"] gives you the right value and at last check you javascript console for errors.
Also HCP_Btn.style.display=""; mean nothing like this.
The problem is probably because Your button's id is HCP_Btn but in the JS further You are accessing it like HCP_btn - the problem could be small b. Also You are missing quotes for the innerHTML value.
Change the line
document.form.HCP_btn.innerHTML='.$row["name"].';
to
document.form.HCP_Btn.innerHTML="'.$row["name"].'";
^ ^ ^
make the b uppercase add quotes ----------^
EDIT: Have You ever tried jQuery? It is commonly and widely used JavaScript framework that makes JS programming so much easier (after You know it)... With jQuery, You could just do:
echo '<script type="text/javascript">
$("#HCP_Btn").css({"display":""}).html("'.$row['name'].'");
</script>';
How is the information from your JavaScript call returned to the innerHTML itself? When does it get called and changed? I think you should do that first.
You have an user pressing a button. Then you go to the database using PHP (need a new request response for that), with AJAX or JavaScript you could make it work client side.
I think that you are mixing up server side and client side issues. You should at least need a function call on the onClick event to toggle the display of the button and show the information. That onClick event should call a JavaScript function and that will handle the change.

Get edited cell value from table cell

I am displaying a table using php on an html page. I want to edit the cell contents (basically make changes to a value, and make ajax call to update the database). The requirement for me is to use an onblur function (I can get around with using onkeypress as well). Can anyone please tell me what would be the simplest way of getting the new value entered with the cell id?
The alert for x tells me its "object HTMLTableCellElement"
Thanks!!!
foreach($_RESPONSE['VENDOR_LIST'] as $r){
echo "<tr><td>".$r['fdEmail']."</td><td id='companyname_".$r['fdId']."' contenteditable='true' onblur='updateValue(id)';>".$r['fdCompanyName']." ".$r['fdId']."</td></tr>";
}
Javascript method
function updateValue(thisdata){
alert(""+thisdata);
var x=document.getElementById(""+thisdata);
if(x){
alert(x);
//var r = x.value;
}
else
alert("not found");
}
In your HTML, change updateValue(id) to updateValue(this.id);
Then put this inside your first if statement in the updateValue function.
if(x.innerText){
alert(x.innerText);
} else {
// Firefox support
alert(x.textContent);
}
The "value" of a table cell is not actually considered a value by javascript, it's just HTML. Try alerting x.innerText. See this question for obtaining cell contents by ID.
Also, I know everyone hates hearing this, but this would be much easier with jQuery.
In your code
var x=document.getElementById(""+thisdata);
Here x is an object, you need to write the required function to change the values or to get the values.
alert(x.innerHTML);
will give you the value of the cell.
Id mentioned in HTML td tag and the id in your getElementById deos not match.
(Use error console in your browser to get the javascript errors)
You may have to change your function like this
function updateValue(thisdata){
var x=document.getElementById("companyname_"+thisdata);
if(x){
alert(x.innerHTML);
}
else
alert("not found");
}
Your HTML code to :
foreach($_RESPONSE['VENDOR_LIST'] as $r){
echo "<tr><td>".$r['fdEmail']."</td>
<td id='companyname_".$r['fdId']."' contenteditable='true' onblur="updateValue('companyname_".$r['fdId']."');">".$r['fdCompanyName']." ".$r['fdId']."</td></tr>";
}
NOTE : I have not tested the code please make changes accordingly, Post here if you find it difficult :)
ALWAYS USE jQuery, It makes your task simple :)

Ajax load content with jquery (URL trigger)

I'm trying to create a demo bar just like wordpress theme companies do
.So far, i was able to achieve a bit of it with
$('#window').load('index.php?action=theme&themeID=5');
Now i want to be able to create specific URLs which trigger specific code.
For example, the above mentioned code runs if i go to
http://mysite.com/theme.php?id=5
How do i do that? is there any way to do the same with php?
Easily done with PHP, you just have to echo the $_GET['id'] value inside your script:
echo "<script type='text/javascript'>$('#window').load('index.php?action=theme&themeID=".$_GET['id']."');</script>";
or
<script type='text/javascript'>
$('#window').load('index.php?action=theme&themeID=<?php echo $_GET['id'] ?>');
</script>
Reference
And yes, GET parameters are unsafe, just as POST parameters may also be manipulated. You should valid to check if the parameter being passed is valid in your PHP page beforing echoing it directly to your script.
From what I gather you are trying to dynamically change javascript based on the url of the page?
<script type='text/javascript'>
$('#window').load('index.php?action=theme&themeID=<?php echo $_GET['id']; ?>');
</script>
You might want to cast the value to an int or something though in order to secure things a bit.

Passing data from an echoed php

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.

how to pass a php values to javascript?

here is the code??
posting code:
$.post('get.php',{selected:"aaaa"},function(return){alert(return);});
when i check the values of "selected" value using
<?php
$r=$_POST['selected'];
echo $r;
?>
is displays the value "aaaa" correctly..
this code works fine...
<?php
$r=$_POST['selected'];
?>
var answer="<?php echo "welcome" ?>";
when we echo the value"welcome" it is stored in the variable answer.and i could print that...
but when i put like this....
<?php
$r=$_POST['selected'];
?>
var answer="<?php echo $r ?>";
an empty value is stored in answer... and nothing gets displayed....
whether specifying $r inside " " is not right... how to specify that......
Assuming that the php code you are showing, is located in get.php, there is no use of using javascript in that same file. If you want to get the returned value in a javascript variable in your page, you need to use the first php snippet and use the return value in your .post function:
javascript in original page:
$.post('get.php',{selected:"aaaa"},function(data){
var answer = data;
});
get.php
<?php
$r=$_POST['selected'];
echo $r;
?>
$_POST['selected'] is probably empty to start with. Make sure you're sending a nonempty value for selected, and that you're using POST. (The easiest way is to look in your browser's developer tools for the initial request).
Note that directly outputting user input into the page introduces a Cross-Site Scripting Vulnerability: The input "; alert("evil"); can show that. Assuming you're using UTF-8 all around, you can write:
var answer = <?php echo json_encode($_POST['selected']); ?>
Also, there are often better ways to transfer data from php to JavaScript, including XHR requests/JSON or data-* attributes.

Categories