php and Javascript Help

easyrider439
easyrider439 used Ask the Experts™
on
Hey Experts, How can I get that php to work in this JavaScript script that I wrote. To my understanding php is executed during loading and variables are loaded into the browser cant I take that php variable and make it a JavaScript variable or How would That work? Hope that makes sense.

in my code I have a php variable echoed and it doesnt work what can I do?
function addRow(divname) {
    var totalrows = document.getElementById(divname).rows.length-2;
    var newdiv = document.createElement('tr');
    var products = '<?php echo $products; ?>';    
    newdiv.innerHTML = '<td>' + (totalrows + 1) + '</td>';
    newdiv.innerHTML += '<td>' + products + '</td>';
    document.getElementById(divname).appendChild(newdiv);    
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Your code itself looks fine, but it'll add the same products when you add a new row.
If you want to add different products, you can:
1. use php function json_encode( ) to pass an array to javascript, if all the values are determined when loading.
2. use AJAX calls to get a new value from the server every time.

Author

Commented:
the php part doesnt show up, its blank not sure why

Commented:
Try something like:
var products = '<?php echo "OK?", $products; ?>';

If you see "OK?", then $products is empty. Check your php code.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
yea I cant see "ok" or the products, I must be missing something my code is below if anybody wants to look through it and see if anything is wrong. Thanks experts



this is index.php

<?php

$title = "Invoicing Home";

$default1 = 'tabbertabdefault';

if (!include '../header2.inc')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/config.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/functions.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/mysql_connect.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include 'functions/main.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}
if (!include 'functions/products.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

?>

<script type="text/javascript" src="script/add_row.js"></script>
<form action="tableaddrow_nw.html" method="get">

<center>
<table cellpadding="0" cellspacing="0" id="invoice">

<tr>

	<td colspan="4"><center>Client: <?php clients(); ?></center></div></td>

</tr>
<tr>
	<td>1</td>
	<td><?php echo $pro; ?></td>
</tr>
<tr>	
        <td><div id="select"></div></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="1050" border="1">

	<tr>
		<td>
	
			<p>
			<input type="button" value="Add" onclick="addRow('invoice');" />
				<input type="button" value="Remove" onclick="removeRowFromTable()" />
			</p>		


		<td>
			Total:	<input type="text" name="tot" value="0">

		</td>
	</tr>
</table>
</center>

</form>

this is addrow.js

function addRow(divname) {
    var totalrows = document.getElementById(divname).rows.length-2;
    var newdiv = document.createElement('tr');
    var products = '<?php echo `"Ok"`, $pro; ?>';    
    newdiv.innerHTML = '<td>' + (totalrows + 1) + '</td>';
    newdiv.innerHTML += '<td>' + products + '</td>';
    document.getElementById(divname).appendChild(newdiv);    
}

Open in new window

Commented:
Why did you surround "Ok" with grave accents(`)? By doing that, you told PHP to run a shell command "Ok", which probably failed, thus printed nothing.

Author

Commented:
Ok I took those out and its still not working, theres gotta be something wrong with the javascript casue when I put $pro anywhere else in the index page it works just not in the jaavascript. could it be that there in seperate pages and I included them in the index page?
Hi,

in your code, it looks like your javascript is in a seperate file named "script/add_row.js". You're not writing PHP-code into a js-file, are you? You have to put your javascript into index.php
Commented:
Yes, dr Pitter is right.
The js file you included with <script> tag goes directly to the client (browser fetches the file for itself), not through PHP engine.
And the browser does not understand <?php ... ?> tag. Your browser is probably indicating a javascript error - the tiny yellow triangle at the lower left corner of IE, double-click it to see the error message.

Just copy the contents of add_row.js and paste it inside <script> tag without src attribute. That way the part of codes also goes through PHP.

Author

Commented:
Allright I moved the script  to the header index.php of the page and now it doesnt work saying "addRow" needs to be defined also it says an syntax error  unexpeceted number on line 121. any help with that??
<?php

$title = "Invoicing Home";

$default1 = 'tabbertabdefault';

if (!include '../header2.inc')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/config.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/functions.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/mysql_connect.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include 'functions/main.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}
if (!include 'functions/products.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

?>
<script type="text/javascript">

function addRow(divname) {
    var totalrows = document.getElementById(divname).rows.length-2;
    var newdiv = document.createElement('tr');
    var products = '<?php echo $pro; ?>';    
    newdiv.innerHTML = '<td>' + (totalrows + 1) + '</td>';
    newdiv.innerHTML += '<td>' + products + '</td>';
    document.getElementById(divname).appendChild(newdiv);    
}

function removeRowFromTable()
{
  var tbl = document.getElementById('invoice');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

</script>


<form action="tableaddrow_nw.html" method="get">

<center>
<table cellpadding="0" cellspacing="0" id="invoice">

<tr>

	<td colspan="4"><center>Client: <?php clients(); ?></center></div></td>

</tr>
<tr>
	<td>1</td>
	<td><?php echo $pro; ?></td>
</tr>
<tr>	
        <td><div id="select"></div></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="1050" border="1">

	<tr>
		<td>
	
			<p>
			<input type="button" value="Add" onclick="addRow('invoice');" />
				<input type="button" value="Remove" onclick="removeRowFromTable()" />
			</p>		


		<td>
			Total:	<input type="text" name="tot" value="0">

		</td>
	</tr>
</table>
</center>

</form>

Open in new window

your javascript - without the php - works on my computer (tested it with chrome and firefox). So there meight be a problem with the php part. How does the source-code of index.php look like, when you let your browser display it?
Commented:
Maybe $pro has a single quote in it?

Try this:
var products ='<?php echo str_replace("'", "\\'", $pro) ?>';

First argument of str_replace is double quote - single quote - double quote,
Second one is double quote - backslash - backslash - single quote - double quote.

Author

Commented:
yea its something in the php stopping it from displaying

Author

Commented:
got it to work I needed to use the str_replace string like jhp333 suggested and I rewrote my php script Thanks Guys I owe ya

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial