I need help on some update like function:
<html>
$result = mysql_query("SELECT *FROM tbl_a LEFT JOIN tbl_b lass ON tbl_b.b_id=a.class_id LEFT JOIN category ON category.category_id=tbl_a.category_id WHERE list ='{$id}'"); </br>
while($row = mysql_fetch_array($result)){
$id_list = $row['id'];
$name = $row['name'];
....
}
}
echo "<script type='text/javascript'>
var id = document.getElementById('list').value = '.$id_list;'
var name = document.getElementById('fname').value ='.$name;'
</script> ";
</html>
my problem is that i can retrieve data but it not displaying on my input elements it should be like an update function
Quotes, quotes...
echo
"<script type='text/javascript'>
var id = '".$id_list."',
name = '".$name."';
document.getElementById('list').value = id;
document.getElementById('fname').value = name;
</script> ";
Working example: http://codepad.viper-7.com/3eI3Od
You need to call your input elements and then give them the new values instead of setting variables like you are doing. Remove the "var something =" and just do document.get.......
If what you posted is the complete code, then there is no list and fname elements. Better open the page and view the source code to see if you have the right value written into your Javascript.
NOTE:
Remember that if you put your Javascript after the element HTML, Javascript cannot retrieve the element. For example:
<script>
document.getElementById('test').value = 'Hello World';
</script>
<input type='text' id='test' value=''>
As you can see, it does not assign value to test element. Solution,
<input type='text' id='test' value=''>
<script>
document.getElementById('test').value = 'Hello World';
</script>
Put the input before the Javascript.
Related
I am using following code to generate multiple textbox on label click, it works correctly and multiple textbox created with different name. I want to take the value of textbox at php side and run insert query for each textbox. But i don't know how to use it in php. Here is my code plz help.
<html>
<head>
<title>
</title>
<script>
var index = 1;
function insertRow(){
var table=document.getElementById("rep1");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "txtName"+index;
t1.name = "txtName"+index;
cell1.appendChild(t1);
index++;
}
</script>
</head>
<body>
<label id="btnAdd" style=" width: 10px;" class="button-add" onclick="insertRow();">add</label>
<table id="rep1">
</table>
</body>
</html>
SQL Schema :
T_no(AI) Text_value
PHP CODE:
<?php
if(isset($_POST["submit"]))
{
$t1=$_POST['name'];// i am confused here how to take value and run query for all
?>
Client side Code
You must use Array for multiple name field
<script>
var index = 1;
function insertRow(){
var table=document.getElementById("rep1");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "txtName"+index;
t1.name = "txtName[]"; //Use array of names not id
cell1.appendChild(t1);
index++;
}
</script>
Please check How to get form input array into PHP array
And You can fetch those array server side using $_POST['txtName']; or any other request method
Server side coding for inserting array into database
<?php
if(isset($_POST["submit"]))
{
$t2=$_POST['txtName'];
$query_parts = array();
$qu = "INSERT INTO try VALUES";
foreach($t2 as $val)
{
$query_parts[] = "('', '" . $val . "')";
}
$qu .= implode(',', $query_parts);
$res=mysqli_query($con,$qu);
if(!$res) { echo("Error description: " . mysqli_error($con)); }
}
?>
The above code will not prevent data from SQl injection.You can use prepared statement for prevent SQL injection in PHP. Link
Use this code. It will helps you.
<?php
print_r($_POST['txtName']); // Here you can get you all values.
?>
<html>
<head>
<title>
</title>
<script>
var index = 1;
function insertRow(){
var table=document.getElementById("rep1");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "txtName"+index;
t1.name = "txtName[]"; // Use array in textbox name..
cell1.appendChild(t1);
index++;
}
</script>
</head>
<body>
<form method="POST" action="" >
<label id="btnAdd" style=" width: 10px;" class="button-add" onclick="insertRow();">add</label>
<table id="rep1">
</table>
<input type="submit">
</form>
</body>
</html>
For this you have to store total index value also(that should specify how many time you created input field).After that your php code should be like below..
PHP CODE:
<?php
if(isset($_POST["submit"]))
{
$t1=$_POST['txtName'.$index];
?>
Where index variable will your current index value from your loop for length of total input fields
I have an input box (name : state_name id : state_name) which is autocompleted with jquery and ajax.I am using "bsn.AutoSuggest_2.1.3" jquery plugin.
This is the html.
<input type="text" name="state_name" id="state_name" />
<input type="text" name="s_n_state" id="s_n_state" value="<?=$letter_1?>"/>
This is the jquery code
<script type="text/javascript">
var options = {
script:"includes/search_state.php?json=true&limit=6&",
varname:"state_name",
json:true,
shownoresults:false,
maxresults:6,
callback: function (obj) { document.getElementById('s_n_state').value = obj.id; }
};
var as_json = new bsn.AutoSuggest('state_name', options);
</script>
Below is search_state.php code -
<?
include_once 'connection.php';
header("Content-Type: application/json");
$state_name = $_GET['state_name'];
echo "{\"results\": [";
$arr = array();
$result = mysql_query("select * from states where state_name like '%$state_name%'") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$arr[] = "{\"id\": \"".$row['state_id']."\", \"value\": \"".$row['state_name']."\"}";
}
echo implode(", ", $arr);
echo "]}";
?>
I am getting the response from ajax when I see it in console of browser but it is not visible in web page and also I want to store state_id from response to s_n_state input box so I can use it.
I had similar problem with autocomplete. My workaround was to store the state_id using delimited character concatenated with the other value.e.g "key:value". On the server side, simply parsed it out. Presumably, no other delimiting character in your key or value. It's not the most elegant but works great for most of my situation.
I have a bunch of image names that is being retrieved from my data base and is in a while loop. In this loop I have a small form with the image names. This form is being use for another purpose. what I want is to get the field information with the item name to a javascript.
Example :
while($row = mysql_fetch_array($query)) {
$itemname = $row['name'];
echo "< input type='hidden' name='$itemname' value='$itemname'>
<img src='source' onclick='getname()' width='100%' height='100%' />";
}
I believe the reason why every image I click on is only giving me the first information from the database is because my id is being duplicated. My question is how can I get the field information to javascript without using the getElementById?
Use the following altered PHP:
$i = 0;
while($row = mysql_fetch_array($query)) {
$itemname = $row['name'];
echo "<input type='hidden' name='".$itemname."[]' id='input$i' value='$itemname' />";
echo "<img src='source' onclick=\"getname('input$i')\" width='100%' height='100%' />";
$i++;
}
Then you can retrieve the input value in Javascript:
function getname(id) {
var theinput = $('#'+id).val(); // jQuery example
// do something
(I also changed the input name to be an array, of couse you could name it what you want, maybe $itemname$i could be an idea, it depends how and if you want to process your form, however the name should be unique or array for good practice)
Here is a working example of HTML/JS: http://jsfiddle.net/fuHSv/1/
How about using some jQuery 1.8.2 like the following:
$(document).ready(function() {
var inputs = $('input').on('click', function() {
var name = $(this).attr('name');
var val = $(this).val();
// do stuff
});
});
See api.jquery.com for more.
im trying to get the id of the picture the user selected to rate with the onclick,however it will only alert "null". when the user selected the div tag, it will alert the id. i want to display the id. i can build off of this and then implement ajax to rate the picture. But, i need help on getting the id of the picture the user selected.
<?php
mysql_connect('localhost','root','');
mysql_select_db("ajax");
$query="SELECT * FROM xxxx";
$result= mysql_query($query);
while($row= mysql_fetch_array($result)){
$rating=$row['ID'];
echo "<img src='".$row['filepath']."'>";
echo "<br>";
echo "<div id='".$row['ID']."' value='".$row['ID']."' onclick='getrating();'>Likes: ".$row['Likes']."</div>";
echo "<br>";
}
im trying to get what picture the user selected to rate.
?>
<script type="text/javascript" >
function getrating(){
var x= document.getElementById("<?php echo $row['ID']; ?>");
alert(x)
}
</script>
A few things that you should do:
Try giving an id which starts with a letter for example, when writing the HTML code use:
echo "<div id='img" . $row[ 'ID' ] . "'></div>"
Note that i've added a prefix 'img' to the div's ID.
You have to pass the correct index to the javascript function because you are drawing the divs in a list but not the javascript function:
onclick='getrating( " . $row[ 'ID' ] . " );'
Now the javascript function will get the correct image ID when called upon clicking the respective DIV element.
Change the javascript function to:
<script type="text/javascript" >
function getrating( id )
{
var x = document.getElementById( "img" + id );
alert( x );
}
</script>
onclick="getrating(this.value);"
That code will pass the current value attribute of the div to the function you made in JavaScript. Be sure to add a parameter to your function to receive the data.
Easy.
Change this:
echo "<div id='".$row['ID']."' value='".$row['ID']."' onclick='getrating();'>Likes: ".$row['Likes']."</div>";
to this:
echo "<div id='".$row['ID']."' value='".$row['ID']."' onclick='getrating(this.value);'>Likes: ".$row['Likes']."</div>";
And this:
function getrating(){
var x= document.getElementById("<?php echo $row['ID']; ?>");
to this:
function getrating(row_id){
var x= document.getElementById(row_id);
You should put your event handler inside image like this:
<img src="" onclick="getrating(this.value)">
And you function should be like this:
function getrating(row_id){
x = document.getElementById(row_id);
alert(x);
}
Javascript:
var counter = 1;
var limit = 5;
function addInput(divName){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = " <br><select name='vehicle[]' id = 'vehicle'><option value = ''>Vehicle "+ (counter + 1) +"</option><option value = '.$brand.' '.$name.'>'.$brand.' '.$name.'</option>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
PHP/HTML:
<script type = "text/javascript" src="js/addinput.js"></script>
<form name="form1" method="POST" action="services.php" onsubmit="return valid()">
<br><br><br><center>
<table class="form" border=1>
<tr>
<td class="head" colspan="2" >Select Vehicle:</td>
</tr>
<tr ></tr>
<tr>
<td colspan="2" class="info">
<div id="dynamicInput">
<br><select name = "vehicle[]" id = "vehicle1">
<option value = "">Vehicle 1</option>';
include_once "vehicledbconnect.php";
$queryveh = mysql_query("SELECT * FROM vehicletbl");
while($fetch_2 = mysql_fetch_array($queryveh)) {
$brand = $fetch_2['vehbrand'];
$name = $fetch_2['vehname'];
echo '<option value = "'.$brand.' '.$name.'">'.$brand.' '.$name.'</option>';
}
echo '</select>';
echo '<input type="button" value="Add another vehicle" onClick="addInput(\'dynamicInput\');"></div>';
Hi. Is it possible to insert PHP values in a javascript? I have a program here that if the customer click the submit button (echo '), a new drop-down form will appear. And I want the drop down form to contain all of the values of the query ($queryveh = mysql_query("SELECT * FROM vehicletbl");). In my default drop-down form, all values of the query are shown. Please help me guys. I am desperate for an answer. Javascript is my weakness. Thanks a lot.
edit:
newdiv.innerHTML = " <br><select name='vehicle[]' id = 'vehicle'><option value = ''>Vehicle "+ (counter + 1) +"</option>" + "<?php include 'vehicledbconnect.php'; $queryveh = mysql_query('SELECT * FROM vehicletbl'); while($fetch_2 = mysql_fetch_array($queryveh)) { $brand = $fetch_2['vehbrand']; $name = $fetch_2['vehname']; <option value = '.$brand.' '.$name.'>'.$brand.' '.$name.'</option> }?>";
Can this be the solution? I've tried but it's not working, if this can be the solution, maybe there's only something wrong with my code here.
The only way to retrieve values from a server from javascript is to use AJAX.
Well you can do it without AJAX if you don't mind a page refresh, but I don't think that is what you want.
I would use a jQuery load function. This is the simplest example I can muster up for you.
You will need to download jQuery (http://docs.jquery.com/Downloading_jQuery) and include it in your html header:
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
Then you can make a simple function to call; either as a onclick or onchange depending on your preference.
function reloadDropDown()
{
document.getElementById('dynamicInput').innerHTML = 'Loading ...';
var v_name = document.formname.elementname.value;
$('#dynamicInput').load("dropdownload.php", { vehicle_name : v_name });
}
Let me go through this. dropdownload.php would have your '$queryveh' made drop down code. Javascript basically plonks whatever happens in dropdownload.php on to a div with the id 'dynamicInput' When javascript loads dropdownload.php it sends via POST a variable by the name vehicle_name which you can use as $_POST['vehicle_name'] within dropdownload.php.
So, dropdownload.php may look something like this.
<?php
$queryveh = mysql_query("SELECT * FROM vehicletbl WHERE vehname = '{$_POST['vehicle_name']}'");
// collect the data and put it in to an Array I like to do this so I can check the array to make sure it has something in it if not return an error message but I will skip that for the purpose of this explanation.
while($ucRow = mysql_fetch_array($queryveh, MYSQL_ASSOC)) array_push($resultsArray, $ucRow);
?>
<select name = "vehicle[]" id = "vehicle1">
<?php
foreach ($resultsArray as $fetch_row){
?>
<option value = "<?php echo $fetch_row['vehbrand'].' '.$fetch_row['vehname'].'; ?>"><?php echo $fetch_row['vehbrand'].' '.$fetch_row['vehname']; ?></option>
<?php } ?>
</select>
?>
I'm not entirely certain on the end result you are after but that is a basic jQuery ajax call. If you can grasp that, you are half way to a truly dynamic web page / app with some further practice with this area. Hope that gives you a direction to go in :)
JavaScript gets evaluated on the client ..so like Html ..so it is to be used the same way.
-> yes, you just use php in your javascript as long as its defined to be evaluated by php first (usually within a .php file)
edit:
just to clarify, if you want to get values within javascript from the server by php.. you need to have a look at what danishgoel said: Ajax (Asynchronous JavaScript) ..see - since Rikudo Sennin disrespected the link, another http://en.wikipedia.org/wiki/XMLHttpRequest ..or even better have a look at a javascript framework that does most of the stuff for you (f.e. jQuery)