I've been trying to understand what is wrong ... but i am still clueless. Any help will be appreciated.
Following code inserts a form element
<?php foreach ($coal_type as $coal) {?>
<div class="checkbox">
<label>
<input type="checkbox" name="usage_checkbox[]" class="usage_checkbox"
value="<?php echo $coal['coal_type_id']; ?>">
<?php echo $coal['coal_type']; ?>
</label>
</div>
<?php } ?>
This data is sent to a form handler which executes following code
obj_params.usage_checkbox = $("input[type='checkbox'][name='usage_checkbox[]']:checked").val();
This data is sent to the controller ( i am using codigniter) which inserts it
$i = 0;
foreach ($this->input->post('usage_checkbox') as $key) {
$coal_type[$i] = $key;
$i++;
}
$coal_type[$i] = $this->input->post('other_usage');
$c_type = implode(',', $coal_type);
$arr_to_insert = array('type_of_coal' => $c_type,);
$insert_id = $this->common_model->insertRow($arr_to_insert,'ekyc_details');
Now when i submit no value is inserted. I have tried everything but I can't figure out what is wrong.
Here multiple values are get from the the checkbox.So you need to serialize to send the values through jquery.The following code will help you.
obj_params.usage_checkbox = $("input[name='usage_checkbox[]']:checked").serialize();
Related
I'm not sure how to search this, but I have an array in php numbered 1 to 20. I have a foreach loop to output the values and have the user be able to click on the numbers. After clicking the number, the page would then output the number clicked.
HTML:
<div id="chapters" onclick="getChapter()">
$array = range(1,20);
foreach($array as $chapter) {
?>
<p class="getChapter" id="currChapter">Chapter <?php echo $chapter;?></p>
<?php
}
?>
</div>
Javascript
function getChapter() {
chapter = document.getElementById("currChapter").id;
document.write(chapter);
}
I'm not able to output the number that the user clicks on.
I have tried putting
id=<?php $chapter?>
, but that does not work as well as replacing id with value and name.
I would recommend passing the $chapter as parameter of a function "Onclick" event :
<div id="chapters">
<?php
$array = range(1,20);
foreach($array as $chapter) {
?>
<p class="getChapter" onClick="getChapter(<?php echo $chapter;?>)">Chapter <?php echo $chapter;?></p>
<?php
}
?>
and the Javascript:
function getChapter(chapterNumber) {
alert(chapterNumber);
}
I don't really understand what you want to do, seems you explanation just a example, but may be this will help. You can call function getChapter with parameter ( onClick="getChapter(<?php echo $chapter;?>) ) :
$array = range(1,20);
foreach($array as $chapter) {
?>
<p class="getChapter" id="currChapter" onClick="getChapter(<?php echo $chapter;?>)">Chapter <?php echo $chapter;?></p>
<?php
}
function getChapter(chapter) {
document.write(chapter);
}
I want to send a php string to javascript script when I click on submit. I already know how to pass variable with json_encode:
var str1 = <?php echo json_encode($str) ?>;
What I want to know is, how to pass a variable which needs to be selected from a dynamic drop down list and which is in a while loop.
<section class="content">
<form id="admin" action="monitor.php" method="GET">
Servers :<br/>
<select name="category" id="category">
<option value="select">Select...</option>
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->keys('*');
$i = 0;
while ($result[$i]!=null)
{
$Addr[$i] = $redis->hget($result[$i], 'Address');
$Port[$i] = $redis->hget($result[$i], 'Port');
$fullAdr[$i] = sprintf ("%s:%s", $Addr[$i], $Port[$i]);
$asdf[$i] = sprintf ("<option value='%s'>%s</option>", $fullAdr[$i], $result[$i]);
echo $asdf[$i];
$i++;
}
?>
</select><br/>
<input type="submit" value="Submit"/>
Now I can't use var str1 = <?php echo json_encode($str) ?>; because I don't know which selection would the user choose.
Each selection contains the IP of a server. I need that IP to pass to a Javascript script when I click on submit.
I am answering it for the future reference, if someone ever encounters the same problem.
I made it work by putting a php on top of the file:
<?php
$JSAddress = $_GET['servers'];
?>
Which I later passed to javascript with:
var str1 = <?php echo json_encode($JSAddress) ?>;
So basically when the user clicks on submit, it sends the request to the php above which stores the value and passes it to javascript.
My checkboxes does not store in the database table that i have
Here is the whole code..
The code that i use for the insert into is this
queryMysql("INSERT INTO patient_diseases (disease,username) VALUES('$disease','$username')");
and my checkboxes code is this...
<?php
$sql = "SELECT name FROM disease";
$query_resource = mysql_query($sql);
while( $name = mysql_fetch_assoc($query_resource) ):
?>
<span><?php echo $name['name']; ?></span>
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>" /><br />
<?php endwhile; ?>
What is wrong with the code?
I had the same code for the insert into for the dropdown list and it worked fine but then i was asked to change it to checkboxes and now it doesnt store the selected values
Also i am getting this error Notice: Array to string conversion in E:\xampp\htdocs\ptixiaki\signup.php on line 50 and the line 50 is this queryMysql("INSERT INTO patient_diseases (disease,username) VALUES('$disease','$username')");
And it store in my data this
Why it stores Array in the disease column and why it doesn't store all the selected checkboxes
The solution was the below..
It was one line of code
$disease = implode(",",$_POST["disease"]);
queryMysql("INSERT INTO patient_diseases (disease,username) VALUES('$disease','$username')");
I have this code from my HTML form.
<?php foreach ($charges as $c): ?>
<br><input type="checkbox" id="charge<?php echo $c->id; ?>" cash="<?php echo $c->amount; ?>"> <?php echo $c->description; ?>
<?php } ?>
when I tick a textbox I want the value for that element pops up.
So far, only the value for the first occurrence pops up.
This is the jQuery code. I know I'm missing something, but i dont know
function triggerChange1(){
$("#ch1").trigger("change");
}
$("#charge1").change(function() {
var v = $(this).attr('cash');
alert(v);
});
if($("#charge1").prop('checked')){
triggerChange1();
}
Firstly you should use a class attribute to group elements together. You should also use data-* attributes to assign any custom meta data to an element as creating your own non-standard attributes will render the page invalid and lead to potential UI and JS issues:
<?php foreach ($charges as $c): ?>
<br><input type="checkbox" class="charge" id="charge<?php echo $c->id; ?>" data-cash="<?php echo $c->amount; ?>"> <?php echo $c->description; ?>
<?php } ?>
Then you can have a single click handler to get the value from the element which raised the event:
$('.charge').change(function() {
var v = $(this).data('cash');
alert(v);
});
// raise the event on checked elements on load:
$('.charge:checked').change();
EDIT: As rightfully stated in Rory's answer you should prefix your custom attributes with data-
What you need is a class.
<?php foreach ($charges as $c): ?>
<br><input type="checkbox" class="charge" id="charge<?php echo $c->id; ?>" data-cash="<?php echo $c->amount; ?>"> <?php echo $c->description; ?>
<?php } ?>
$(".charge").change(function() {
var v = $(this).data('cash');
alert(v);
});
You can see it working here: http://jsfiddle.net/0cude9jr/
function triggerChange1(){
$("#ch1").trigger("change");
}
$(document).ready(function() {
$("#charge1").change(function() {
var v = $(this).attr('cash');
alert(v);
});
if($("#charge1").prop('checked')){
triggerChange1();
}
}
And you should use value attribute and not cash attribute.
And then you can call var v = $(this).val();
It's part of my view. Me need transmit input name in on which i click.
Below is a script that will get the name input after click
<div class="form_hint"><?=form_error('this get value from javascript after click')?></div>
<?php echo form_open("onenews/" . $onenews['id'] . "#signup", $form_reg['main_form']); ?>
<?php echo form_input($form_reg['login'], $this->form_validation->set_value('login')) ?>
<?php echo form_input($form_reg['email'], $this->form_validation->set_value('email')) ?>
<?php echo form_input($form_reg['password']) ?>
<?php echo form_input($form_reg['conf_password']) ?>
<?= MY_Controller:: create_captcha(); ?>
<?php echo form_input($form_reg['captcha']) ?>
<?php echo form_input($form_reg['submit']) ?>
<?php echo form_close(); ?>
</div>
</div>
jq
<script type="text/javascript">
$(function(){
var curInput = '';
$('#form_reg').find('input').on('click', function(){
curInput = $(this).attr("name");
});
})
</script>
Or i must use ajax?
Thanks!
Your question is not clear at all, but I assume you want to dynamically change the content of the form_hint div. You can't do that with PHP. Once PHP renders the page, it shuts down and does nothing more. So the only way to make PHP show that message is after the form submit, but then you lose your click data. There is a way to save the clicked element in a session for example, but that would be a really bad solution.
So the best solution would probably be to list all the hints in a JavaScript variable, and call the appropriate one upon the click event, and fill the form_hint div with it.
$('.form_hint').text(yourAppropriateMessageHere);
An example with a hidden div for the login input field with exactly how you described would be:
<div class="form_text_login"><?=form_error('login')?></div>
JS
var loginMessage = $('.form_text_login').text();
// list others here
// then make a tree with switch/case or if/else
if (curInput == 'login') {
$('.form_hint').text(loginMessage);
}
else if (curInput == 'email') {
// do the same with the email message, etc.
}