value sent to controller by using JS - javascript

I'm new to codiginiter and it's still developing.
I'm trying to send a JS value to codiginiter controller but I'm getting errors. Firebug says syntax error. But when I correct it, another syntax error is being shown.
I think the problem is with my script. Can anyone can figure out what's wrong? All I want is to send username value to method in controller. Following alert is triggered when I press the button (tested and working).
<script type="text/javascript">
$(function(){
$("#postAnswer").click(function(){ // passing down the event
alert("checked");
$.post('<?php echo base_url(); ?>homepage/postanswer', {
//username:document.getElementById('username').value
username:<?php $ques[0]->username ?>
});
});
});
</script>

Correct way to echo PHP variable would be to JSON encode it first (and use echo):
$.post('<?php echo base_url(); ?>homepage/postanswer', {
username: <?php echo json_encode($ques[0]->username); ?>
});

Depending on what the error is with FireBug, it's like an issue of needing to enclose the <?php $ques[0]->username ?> in quotes to make it a string.
username: "<?php $ques[0]->username ?>"

Use append('username',$query[0]->yourdataname)

Related

echo alert() in php show everything in the string. octobercms plugin

I'm having issue when echoing alert() in php. It show everything in the string.
echo "<script type='text/javascript'>alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');</script>";
Output
You can use javascript code between ending and starting php tags like below,
?>
<script type="text/javascript">
alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');
</script>
<?php
// rest of the php code will go here.
?>
OR try this please,
<?php
echo '<script type="text/javascript">';
echo 'alert("Thank you for your enquiry, we will contact you as soon as possible, have a nice day!");';
echo '</script>';
?>
Solution: I fixed it by removing everything. and just echo the message directly without the alert(). I'm not sure why it behave like this because this is my custom plugin. I will update if I found the source of this behavior. Thank you for all the replies.
echo 'Email Successfully Sent. We will reply soon!';

Auto-fill form on WordPress

I am using the Scripts n Styles plugin with this code to auto-fill a logged in user's email into form on WordPress:
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("Element-Name")[0].value = "<?php $current_user->user_email ?>";
}
</script>
However, when I refresh the page I get the following text (the quoted value in the code above) instead of the actual value.
<?php $current_user->user_email ?>
Any idea what I am missing here? Is this an issue with the plugin?
The first thing I see is that you don't output anything.
Try
<?php echo $current_user->user_email; ?>
But in your example it should have been just an empty value. There is something fishy with the PHP parser not detecting the PHP part.

bug php variable to javascript

Ive struggled with a bug which I can simplify to this:-
if I set a variable $test in php and try to access it in javascript it sometimes works, but often doesnt
Im using alert to display the data in javascript... alert( php tag echo $test; close php tag ) if it doesn't show below
<script>
alert(<?php echo $test; ?>);
</script>
example, if in php I set $test="hello"; it does not work. Nothing!
but if I set $test=time(); it does work
why cant I set a simple string and access it in javascript? Its strange it can access a more complicated timestamp but not a string!!
You need to tell JavaScript that you want to alert a string.
alert('<?php echo $test; ?>');
Notice the quotes wrapped around the PHP statement.
You're missing out "".Try this instead.
<script>
alert("<?=$test?>");
</script>
You need to add quotes
alert('<?php echo $test; ?>');

Ajax onchange combobox not working

i know this question has been asked in many another case in this site. But still, i dont have the right answer to solve my problem .
i want to make the ajax function for my combo box and textarea. So when i select an option in the combo box, text inside textarea will change depending on the combo box's selected value.
UPDATED: my textarea code
this is my combobox and textare code :
<select class="form-control" name="option_template" id="template" onchange="get_template(this.value);">
<option value="" selected="" disabled=""> -- Pilih Template --</option>
<?php foreach ($template as $template){
?>
<option value="<?php echo $template['id'];?>"><?php echo $template['nama'];?></option>
<?php }
?>
</select>
<div class="col-md-8 col-sm-8 col-xs-12">
<textarea id="template-content"></textarea>
</div>
this is my javascript function and the ajax
<script type="text/javascript">
// alert("hai");
function get_template(id){
alert(id);
$.ajax({
method:"POST",
url:'<?php echo base_url();?>broadcast/ajax_template',
data:{option:id},
succes:function(msg){
alert(msg);
$('#template-content').val(msg);
}
});
}
</script>
and the last , this is my php function that retrieve the post from ajax
public function ajax_template(){
$id=$this->input->post('option');
$q=$this->M_template->get_template($id)->row_array();
echo "Test output".$q['content'];
}
when i run the code above, the alert(id); in the javascript get_template() function syntax is working, so i get the option value everytime i select the option. But the problem is i cannot get the output data from the ajax's post. Can someone please help me with this? i know this is maybe a basic knowledge but i've spent hours to solve this problem , thank you :)
This is weird and i dont know why my previous code is not working. But i found a new way to do this by using change() function to the select syntax and use the get method instead of post.
this is my javascript and ajax function
$("#template").change(function() {
var e = document.getElementById("template");
var id = e.options[e.selectedIndex].value;
$.ajax({
url: '<?php echo base_url();?>Broadcast/ajax_template?option='+id,
success: function(msg){
$('#template-content').val(msg);
}});
});
i removed onchange attribute of my select syntax
and change the php function to get the id from post to get
$id=$this->input->get('option');
Change your ajax_template() method
You are getting response from M_template as an array. you cannot echo array as like that.
Use echo json_encode($arrayname). in your case echo json_encode($q['content']);
and make sure that in ajax you mention dataType:'json'
Could you please call your function
public function ajax_template(){
$id = $this->input->post('option');
$q=$this->M_template->get_template($id)->row_array();
echo "Test output".$q['content'];
exit;
}
Without using ajax and check it will display anything or not. Please pass static value on $id variable.

How do I embed Javascript in PHP code?

I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "var oldnotes = document.getElementById('oldnotes');";
echo "oldnotes.disabled = false;";
echo "var record = document.getElementById('record');";
echo "record.disabled = false;";
echo "</script>";
}
I have also tried;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "$('#oldnotes').removeAttr('disabled')";
echo "$('#record').removeAttr('disabled')";
echo "</script>";
}
But no luck :(
I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.
This is a better approach for these kind of problems :
if (isset($_POST['edit'])){
?>
<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>
<?php
}
<?
<script language='javascript'>
(javascript code here)
</script>
?>
Try use onclick on your Button
<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">
I hope it helps.
The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.
Here's what I suggest:
<?php
if (isset($_POST['edit'])) {
?>
<script type="text/javascript">
alert("Check entry"); // Use this for checking if your script is reaching here or not
$('#oldnotes, #record').removeAttr('disabled');
</script>
<?php
}
?>
You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.
Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.
If not, something wrong with your edit button and the form POST
Use single quotes for script type
echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
use this jquery code:
<script>
$('#oldnotes,#record').attr('disabled','')
</script>
If you want to use JS with PHP you can read here:
how to write javascript code inside php
However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.
Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.
<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >
Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.
Hope it helps!
you can always make a cookie
use document.cookie()
set the onclick=“” to changing the cookie
and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})

Categories