I need to pass an array as a function argument from php to js.i am getting the values from
database.
while ($rows = pg_fetch_array($qry))
{
?>
<option value="<?php echo $rows['relation_name_local']?>">
<?php echo $rows['relation_name_local']?>
</option>
<?php
$app_relation_array[] = $rows['relation_name_local'];
}?>
i want to pass $app_relation_array[] values through addNewRow() this function
Can anyone please help me with this.
Thanks a lot.
Use json_encode() function and echo it in javascript
while ($rows = pg_fetch_array($qry))
{
?>
<option value="<?php echo $rows['relation_name_local']?>">
<?php echo $rows['relation_name_local']?>
</option>
<?php
$app_relation_array[] = $rows['relation_name_local'];
$new_data = json_encode($app_relation_array[]);
}?>
And in your html inside script tag
<script>
var data = JSON.parse("<?php echo $new_data; ?>");
alert(data);
</script>
Use json_encode function in php
Related
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 5 years ago.
I like to display php array in javascript, but in specific element with id.
below is my code which shows what I want to do.
<?php function ogolne_informacje(){
global $mypod;?>
<strong><?php echo 'Pokój: '?></strong><?php echo $mypod->display('room')."<br />";?>
<strong><?php echo 'Email: '?></strong><?php echo $mypod->display('user_email')."<br />";?>
<strong><?php echo 'Telefon: '?></strong><?php echo $mypod->display('phone')."<br />"; }?>
<?php $i =0;
while ( $mypod->fetch() ) :{
ob_start();
ogolne_informacje();
$output[$i] = ob_get_clean();
$i++;
} endwhile;?>
<div id="test"></div>
<script>
var out = <?php echo $output; ?>;
$(document).ready(function () {
$('#test').html(out.toString());
});
</script>
How can I do that?
Thanks!
You can't loop like that, you need to loop the PHP array and push into javascript array:
<script type="text/javascript" language="javascript">
var pausecontent = new Array();
<?php while ( $mypod->fetch() ) :{
ob_start();
ogolne_informacje();
?>
pausecontent.push('<?php echo ob_get_clean(); ?>');
<?php } ?>
</script>
i have a problem with that i need to get the value from the dropdown list to be a number and the name for a kategory to be the name that the user picks.
<select name="kategori">
<?php
$query=mysql_query("SELECT KategoriID from Kategori");
$second=mysql_query("SELECT KategoriNavn from Kategori");
while($r=mysql_fetch_row($query) && $v=mysql_fetch_row($second)){
echo "<option value='$r[0]>$v[0]</option>";
}
?>
This is the code i have, but i cant make it to work.
Im kinda new to PHP. Thanks!
There's no need to write two different queries. You could have written just a single one. I think mysql fetch_assoc is a tad easier to understand.
You can try something like this:
<?php
$query = mysql_query("SELECT KategoriID, KategoriNavn from Kategori") or die(mysql_error()); // Debugging displays SQL syntax errors, if any.
echo "<pre>";
print_r($query);
exit; // Let me know what the array looks like.
while ($r= mysql_fetch_assoc($query)) { ?>
<option value=<?php echo $r['KategoriID']; ?> >
<?php echo $r['KategoriNavn']; ?>
</option>
<?php } ?>
<?php
echo "<pre>";
print_r($_POST); // Do this where you're checking your POST data
exit;
?>
Assuming you want option value to be KategoriNavn and the option to display to be KategoriID.
Hope this helps.
Peace! xD
I am running a foreach loop and I want to be able to change the content and class of the affected div elements.
foreach ($fields as $key => $value) { ?>
<script type="text/javascript">
document.getElementById('<?php echo $key ?>').innerHTML = '<?php echo $value ?>';
</script>
<?php } ?>
foreach ($fields as $key => $value) { ?>
<script type="text/javascript">
document.getElementById('<?php echo $key ?>').innerHTML = '<?php echo $value ?>';
</script>
you better use it like this .
<script>
<?php foreach ($fields as $key=>$value){ ?>
document.getElementById("<?php echo $key; ?>").innerHTML = "<?php echo $value; ?>";
<?php } ?>
</script>
and make sure you have all correspoinding elements with the same id as $key in this script for example.
<?php foreach($fields as $key=>$value){ ?>
<div id="<?php echo $key; ?>" ></div>
<?php } ?>
Regards
You may use only one part...
server-side with PHP and more tempting
client-side with Javascript
For the last approach, it could be okay to store / request the array as an JavaScript array, or json object and iterate it / update the existing elements during a loop after the site has been loaded.
A mixture of both may lead to a non-maintainable source and more render and execution time as expected.
I have not got what you actually want, But lets with this code. It may help you...
<script type="text/javascript">
$( document ).ready(function() {
<?php
foreach ($fields as $key => $value) {
?>
document.getElementById('<?php echo $key ?>').innerHTML = '<?php echo $value ?>';
<?php
}
?>
});
</script>
I am just getting into the world of PHP, Javascript and HTML and would like some help from the community regarding the following code. Basically I want to pass variables plucked from a ODBC_connection by PHP into textboxes. Lines 1-3 were for me to test to get the box to update which it does but anything echoed by PHP does not run. I am completely new to this so I realize I must be missing something trivial.
I welcome any suggestions or comments about what I can do to fix this or what I can do better in general.
Thank you.
<script type='text/javascript'>
document.getElementById('modeltxt').value = "test2";
</script>
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('modeltxt').value =\"TEST3\";";
echo "document.getElementById('customertxt').value = $customer;";
echo "document.getElementById('endusertxt').value = $enduser;";
echo "document.getElementById(dongletxt').value = $dongle;";
echo "document.getElementById('shipdatetxt').value = $shipdate;";
echo "document.getElementById('chasistypetxt').value = $chasistype;";
echo "document.getElementById('chasisnumbertxt').value = $chasisnumber;";
echo "document.getElementById('opsystxt').value = $opsys;";
echo "document.getElementById('dvd1txt').value = $dvd1;";
echo "document.getElementById('dvd2txt').value = $dvd2;";
echo "document.getElementById('storagetxt').value = $storage;";
echo "document.getElementById('nodrivetxt').value = $nodrive;";
echo "document.getElementById('drivesizetxt').value = $drivesize;";
echo "document.getElementById('interface1txt').value = $interface1;";
echo "document.getElementById('interface2txt').value = $interface2;";
echo "document.getElementById('interface3txt').value = $interface3;";
echo "document.getElementById('interface4txt').value = $interface4;";
echo "document.getElementById('interface5txt').value = $interface5;";
echo "document.getElementById('interface6txt').value = $interface6;";
echo "document.getElementById('commentstxt').value = $comments;";
echo "document.getElementById('warrantyexptxt').value = $warrantyexp;";
echo "document.getElementById('extendedwarrantytxt').value = $extwarexp;";
echo "document.getElementById('onsitetxt').value = $onsite;";
echo "document.getElementById('sqlversiontxt').value = $sqlversion;";
echo "<\script>";
You can create dynamics JS using the below
Define Content-Type on the top of your .js.php file:
<?
header('Content-Type: application/javascript');
// Write your php code
?>
and call the js file like this ..
<script type="application/javascript" src="JS_PATH/name-of-file.js.php"></script>
and if you want to use inline php values, you can write like this
<script type="application/javascript">
document.getElementById('modeltxt').value = "<?php echo $dummy_value ?>";
</script>
No need to do this. You can simply use javascript inside php as below :-
As you mention in your question
Basically I want to pass variables plucked from a ODBC_connection by PHP into textboxes.
<?php
// Php block of code
?>
<script type="text/javscript">
document.getElementById('modeltxt').value = "TEST3";
document.getElementById('customertxt').value = "<?php echo $customer;?>";
......
....
...
</script>
<?php
// Another block of code
?>
this is regarding passing arguments to a function in javascript.
This code is not working. Is it because of the _(underscore) in the $l?? if yes, what change should be brought in the code. Thanks in advance. Please not the $l value will be passed to another page using ajax and queried into a database.
<script>
function show(str)
{
var r=str;
alert(r);
}
</script>
<?php
$l=Somethin_nothin_anythin;
echo "<select onChange='show($l)'>";
echo "<option></option>";
echo "</select>";
?>
it should be:
$l="something_nothing_anything":
echo "<select onChange='show(\'".$l."\')'>";
echo "<option></option>";
echo "</select>";