Javascript not acquiring field value - javascript

I have 2 php pages, htmldisp.php and classfn.php. And an alert used in hrmldisp.php is not working. It should actually give a dropdown value.
htmldisp.php is
<?php include("classfn.php"); $obj=new hello;?>
<script language="javascript">
function submitted(){
var select=document.getElementById('newdrop').value;
alert(select);
}
</script>
<?php
$id="newdrop";
echo $obj->hellofn($id); ?>
<input type="submit" onclick="submitted();"
classfn.php is
<?php
class hello{
function hellofn($id){
$s="select `Name`,`Value` from `Days`;
$q=mysql_query($s);
$p='<td>'.'<select id="$id">';
while($re=mysql_fetch_array($q)){
$value=$re["Value"];
$name=$re["Name"];
$p.='<option value="' . $value . '"' . ( $selected==$value ? ' selected="selected"' : '' ) . '>' . $name . '</option>';
}
$p.='</select>'.'</td>';
return $p;
}
?>
The problem is alert(select) not working. Thanks

As far as I can tell you never call the function submitted. On top of that your HTML is completely malformed. You have a bunch of html and javascript outside of your html tag. And you have 2 opening html tags with no close...

Related

HTML Quotes are making a mess

The result I want:
<div class="cat_cards_item" onclick="myFunction('http://website','name','description')">
I tried 2 methods:
1.
echo "<div class='cat_cards_item' onclick='myFunction(" . $resultwebsite['website'] . "," . $resultname['name'] . "," . $resultdesc['description'] . ")'>"
My result:
<div class="cat_cards_item" onclick="myFunction(http://website,name,description)">```
echo "<div class='cat_cards_item' onclick='myFunction('" . $resultwebsite['website'] . "','" . $resultname['name'] . "','" . $resultdesc['description'] . "')'>"
My result:
<div class="cat_cards_item" onclick="myFunction(" http:="" website="" ',' name="" ',' description=""')'="">
Im trying to call a javascript function that opens a modal with the information I pull from my php code.
I tried most styles of placing quotes and im getting a headache from it, any help?
By using the heredoc-syntax you can get a more readable string, and you can simply integrate your variables without concatenations:
<?php
$mydiv = <<<EOD
<div class="cat_cards_item" onclick="myFunction('{$resultwebsite['website']}','{$resultname['name']}','{$resultdesc['description']}')">
EOD;
echo $mydiv;
?>
If you would create multiple of the same HTML-structure, I would create a simple PHP function that generates the HTML code, so you don't have to worry about the quotes:
<?php
$HTML = createModal('cat_cards_item', "myFunction('http://website','name','description')");
echo $HTML;
function createModal($class, $onClick) {
return "<div class=\"${class}\" onclick=\"${onClick}\">";
}
<div class="cat_cards_item" onclick="myFunction('http://website','name','description')">
Try the PHP code online!

How do I use a js variable in a echo php function call?

My actual application is to do content replacement from a db to an element id. I do it know from text files (1558 of them), and it works great. I* just wanted to try this method. Thank you ahead of time for
This attached code shows what I've tried. The echos in the php function show I make it into the function. The alerts show the sending values and return values work.
<html>
<head>
<script type="text/javascript">
var b = "MyText.txt";
</script>
</head>
</html>
<?php
function add($str){
echo "My strtest file is : " . $str . " ";
echo " ";
$MyTexkFile = "<html><head></head><div> <p> This is my html text display </p></div id = \'mydiv\'><body><body></html>";
echo "The string is " . " " . $MyTexkFile . " ";
$newstr = $str . " Add some text to str";
//return $MyTexkFile ;
//return $str;
return $newstr;
}
?>
<script>
function phpadd(b) {
alert(b);
var a = "1234";
alert(a);
var padd = '<?php echo add("3579.txt")?>';
console.log(padd );
}
</script>
<button onclick='phpadd("4567.txt")'>add</button>
As I said above, the code works properly until I try to pass a js variable (a or b). I've tried single and double quotes. With and without tilde.
My error: SyntaxError: '' literal not terminated before end of script jsToPhpFunction.php:15:97. Why is this? Can I do anything to get the variable to pass?
Not sure if this is what you're trying to achieve?
<?php
if(isset($_POST['submit'])){
$str = $_POST['hiddenVal'];
echo "My strtest file is : " . $str . " ";
echo " ";
$MyTexkFile = "<html><head></head><div> <p> This is my html text display </p></div id = \'mydiv\'><body><body></html>";
echo "The string is " . " " . $MyTexkFile . " ";
$newstr = $str . " Add some text to str";
}
?>
<script>
function phpadd(a) {
document.getElementById("hiddenVal").value = a;
}
</script>
<form action="" method="post">
<input type="text" name="hiddenVal" style="display:none" id="hiddenVal" />
<button name="submit" onclick='phpadd("4567.txt")'>add</button>
</form>

Javascript variable not working in php echo

My code :
<?php echo ' <script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques".p);? >
The value of p is displayed as 0.
You need to close your php tag properly as well as the <script> tag like so:
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques" +p);</script>'; ?>
Also, change the . to a + as you are concatenating in javascript not PHP
The correct answer is:
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques" + p)'; ?>
Strings in single quotes will be escaped, use quotation marks instead.
<?php echo "<script>var p=0;for(var i=0;i<=5;i++){p++;}alert('ques' +p);</script>"; ?>
The mistakes in your code are,
Close your php tag properly - ?> instead of ? >
Close the script tag in the end - </script>
Close the single quote you started with echo
The correct code would be
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){ p++; } alert("ques" + p); </script>'; ?>

Problems with PHP variables in the URL of a JavaScript function

I'm using the following script to use a popup window to load a $_GET address:
while ($row1 = $sth1->fetch(PDO::FETCH_ASSOC)){
if($_SESSION['scontact_id'] == 'scontact_id' && $row1['shared_id'] != '' && $row1['shared_id'] != '0'){
echo "<td> <center><button onclick='myFunction()'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center>
<script>
function myFunction(shared_id) {
window.open('". $mywebsite . "vcontact_id=" . $row1['shared_id'] ."' +shared_id, '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script> </td>";
However, it only loads the first row in the loop correctly. After that, it displays the same $_GET address, regardless of where it is in the while loop. It does the same thing if I convert the row to a php variable and pass the PHP variable, to the script, as a JavaScript variable. It works fine, without the popup, like this:
echo "<td>"."<a href='" . $mywebsite . "vcontact_id=" . $row1['shared_id'] . "' target='_blank', '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400'><center><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></center></a>" . "</td>";
I'm not very familiar with JavaScript. Any help would be greatly appreciated.
Chage this part:
echo "<td> <center><button onclick='myFunction()'>
for this:
echo "<td> <center><button onclick='myFunction(\"" . $row1['shared_id'] . "\")'>
EDIT: Or make it one line instead of using a function:
echo "<td> <center><button onclick='window.open(\"". $mywebsite . "vcontact_id=" . $row1['shared_id'] ."\", \"_blank\", \"toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400\");'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center></td>";
myFunction is being overwritten each time in the loop. You could always rewrite the code with an increment counter to change the function name. It's typically better to not mix javascript in php in my opinion. You could always just pass these in as additional parameters into your function where you are outputting php / js.
You are continuously redefining the function: myFunction(). you should output the script tag once, outside the loop, and change the onclick attribute on your button to ...<button onclick='myFunction(" . $row1['shared_id'] . ")'>..., if I am reading your problem correctly
Please try below code:
<?php
while ($row1 = $sth1->fetch(PDO::FETCH_ASSOC)){
if($_SESSION['scontact_id'] == 'scontact_id' && $row1['shared_id'] != '' && $row1['shared_id'] != '0'){
echo "<td> <center><button onclick='myFunction(".$row1['shared_id'].")'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center></td>";
}
}
?>
<script>
var mywebsite = "<?php echo $mywebsite ?>";
function myFunction(shared_id) {
window.open(mywebsite+"vcontact_id="+shared_id, '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
I believe you simply need to move your script outside the if loop, yet maintain it in the while loop and you need to add your (shared_id) that is required by the function, to the click event.
<?php
while ($row1 = $sth1->fetch(PDO::FETCH_ASSOC)){
if($_SESSION['scontact_id'] == 'scontact_id' && $row1['shared_id'] != '' && $row1['shared_id'] != '0'){
echo "<td> <center><button onclick='myFunction(".$row1['shared_id'].")'><img border='0' alt='Contacts' src='/Welcome/modules/mod_crmsearch/images/peoplesmall.png'></button></center></td>";
}
echo "
<script>
function myFunction(shared_id) {
window.open('". $mywebsite . "vcontact_id=" . $row1['shared_id'] ."' +shared_id, '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
";
}
If you care at all about page optimization, it would be far wiser instead of looping the script in the while statement, to set a data variable and a class on the button and use an onclick event to determine the values so you don't have 20 instances of the same script with different values on the page.
For example:
Inside your button:
class='clicked' data-id='".$row1['shared_id']."'
Then use jquery for the script
<script>
$( ".clicked" ).click(function() {
var data = $(this).attr('data-id');
window.open('". $mywebsite . "vcontact_id='+data', '_blank', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
This looks for a click event on the class "clicked" which we assigned to the button. When button is clicked, we get the value of "data-id" and set it as a variable "data". We then call that variable instead of echoing a php variable. This allows us to take the script out of the loop and not write it to the page multiple times.

PHP get something from a html file, then pass something back to the original html file

I am very new to php,
Suppose, I have a index.html, and a cal.php, in a directory.
index.html:
<head>
<script>
some java script here used to display graphs based on cal.php calculated results
</script>
</head>
<body>
<form action="cal.php" method="post">
<div class="row x_title">
<div class="col-md-6">
<h3>Dashboard</h3>
</div>
<select class="form-control" id="myFileSelect">
<?php
echo '<option>Choose file</option>';
foreach (glob('Report' . '/*.csv') as $file) {
list($af, $bf) = split("/", $file, 2);
list($filenam, $extnt) = split(".", $bf, 1);
echo '<option value="' . $bf . '">' . $filenam . '</option>';
}
?>
</select>
</div>
<input type="submit">
</form>
</body>
cal.php
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c = 0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
$output
/* pass the output back to original html */
/* let's say pass this php variable $output to the javascript on original html */
?>
I want to post the form when user hit submit, pass some value to cal.php, doing some calculations in the cal.php file, then pass the variable $output to original html's JavaScript variable.
I'm kind of blank on between how php works with a html page.
Some one could help me? Or give me some ideas?
Using ajax you can do it. The ajax request from index.html will be calling cal.php The cal.php should echo the result you wantand in the success function of ajax you get the response.

Categories

Resources