?
Solved

adding PHP inside javascript query

Posted on 2013-01-25
14
Medium Priority
?
174 Views
Last Modified: 2013-02-04
I may be doing something that is not possible, but I am trying to use a script to add new forms elements using javascript.  One of my form elements I need to duplicate is a <select> box being populated from the DB.  It works fine in the HTML part of the form, but once i try to setup in the JS as an added row, it breaks.  

I know very little about JS so any help is greatly appreciated.  Here is my function to add the row with each element.

function displayResult()
{
var table=document.getElementB<wbr ></wbr><wbr ></wbr>yId("depos<wbr ></wbr>itsubmit")<wbr ></wbr>;
var row=table.insertRow(-1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
var cell4=row.insertCell(3);
var cell5=row.insertCell(4);
cell1.innerHTML="<select name=\"pmntFrom[]\" style=\"min-width:100px\"><wbr ></wbr><wbr ></wbr><option selected=\"selected\"></op<wbr ></wbr><wbr ></wbr>tion><opti<wbr ></wbr>on value=\"1\">Congregation Giving</option><option value=\"2\">Outside Fundraising</option><optio<wbr ></wbr><wbr ></wbr>n value=\"3\">Denominational<wbr ></wbr><wbr ></wbr> Support</option><option value=\"4\">Event Registrations</option><opt<wbr ></wbr><wbr ></wbr>ion value=\"5\">Reimbursements<wbr ></wbr><wbr ></wbr>/Refunds</<wbr ></wbr>option><op<wbr ></wbr>tion value=\"6\">Cafe/Book Sales</option></select>";
cell2.innerHTML="<select name=\"desigAcct[]\" style=\"min-width:100px\"><wbr ></wbr><wbr ></wbr><option><?php echo $account; ?></option></select>";
cell3.innerHTML="New";
cell4.innerHTML="New";
cell5.innerHTML="New";
}    
</script>

Open in new window


Line 11 is where I am trying to build the <select> box.  I've tried several variations of quotations, etc. but to no avail.
0
Comment
Question by:axessJosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38820430
php need to be interpreted by a web server which support PHP language. Once you web server see a file with php it know he can found php code inside <?php ?> tags

your web browser know nothing about php, putting php inside php tags mean nothing than put/add it to the page.

you may use ajax to achieve your goal
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38820675
In addition, those <wbr> tags are screwing up your code.  In particular, you can't put them in the middle of other HTML tags like this: "</op<wbr ></wbr><wbr ></wbr>tion>"
Where are they coming from?
0
 
LVL 2

Author Comment

by:axessJosh
ID: 38820680
I saw that when I added the script to EE.  I don't have those in my script, i think its replacing my forward slashes I'm using to escape the quotation marks with that.
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38820713
I've never seen EE do that.  Didn't do it here...  What editor are you using to code your files?

function displayResult()
{
var table=document.getElementById("depositsubmit");
var row=table.insertRow(-1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
var cell4=row.insertCell(3);
var cell5=row.insertCell(4);
cell1.innerHTML="<select name=\"pmntFrom[]\" style=\"min-width:100px\"><option selected=\"selected\"></option><option value=\"1\">Congregation Giving</option><option value=\"2\">Outside Fundraising</option><option value=\"3\">Denominational Support</option><option value=\"4\">Event Registrations</option><option value=\"5\">Reimbursements/Refunds</option><option value=\"6\">Cafe/Book Sales</option></select>";
cell2.innerHTML="<select name=\"desigAcct[]\" style=\"min-width:100px\"><option><?php echo $account; ?></option></select>";
cell3.innerHTML="New";
cell4.innerHTML="New";
cell5.innerHTML="New";
}    
</script>

Open in new window

0
 
LVL 2

Author Comment

by:axessJosh
ID: 38820719
Dreamweaver.  I had to go through and edit the code after i copied and pasted and noticed that looked weird.

Looks like parsing PHP in javascript may be impossible.  

any ideas?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38820725
you write/copy your code to/from the html pane
0
 
LVL 2

Author Comment

by:axessJosh
ID: 38820731
yes, leakim971
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38820734
No, the whole page must be a PHP page and then PHP will echo the value at that point when it generates the page.  But you are right if you mean running PHP in javascript After the page is loaded.  That doesn't happen.  As @leakim971 suggested above, you can use AJAX methods to get a values from PHP on the server but that does require different code.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 38821244
Is there a way to add a new row with all the form elements without having to set the form elements in the JavaScript function?  More like a div that just contains the elements?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38821261
Save this as my_ajax_php_script.php
<?php

$account = $_REQUEST["account"]; // later replace it by a $_POST

echo "<tr>";

echo "<td><select name=\"pmntFrom[]\" style=\"min-width:100px\"><option selected=\"selected\"></option><option value=\"1\">Congregation Giving</option><option value=\"2\">Outside Fundraising</option><option value=\"3\">Denominational Support</option><option value=\"4\">Event Registrations</option><option value=\"5\">Reimbursements/Refunds</option><option value=\"6\">Cafe/Book Sales</option></select></td>";

echo "<td><select name=\"desigAcct[]\" style=\"min-width:100px\"><option>$account</option></select></td>";

echo "<td>New</td>";
echo "<td>New</td>";
echo "<td>New</td>";

echo "</tr>";

?>

Open in new window

in you php script/page, you need to pass the $account, we can start by using an hidden field but it would be better to use a session variable but for now forget
So in you page, you have an hidden field :
<input type="hidden" id="the_account" value="<?php echo $account; ?>" />

Open in new window


Now to load your row, we're going to use ajax, to simplify the code and the readilbilty we use jQuery. So add the plugin inside your <head> section
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

Open in new window

and AFTER THE PLUGIN SCRIPT TAGS replace your javascript displayResult function by this one :
<script type="text/javascript">
function displayResult()
{
    $("#depositsubmit").load("my_ajax_php_script.php", { account: $("#the_account").val() });
}
</script>

Open in new window


if that don't work, please provide a link to see your page
0
 
LVL 2

Author Comment

by:axessJosh
ID: 38852231
leakim971,

I can see where this is going to work, but i need a few tweaks.

when I click on the #depositSubmit button it does add the row, however it also removes the other rows of the table that were already there.

also, I am not sure where to put the hidden input field you have shown to get he $account variable.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 38852254
this is working to add a row, however it removes any rows currently showing.  I need this to add a new row to the table, not add and remove the existing.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 38852310
replace :
$("#depositsubmit").load("my_ajax_php_script.php", { account: $("#the_account").val() });

Open in new window

by :
$.post("my_ajax_php_script.php", { account: $("#the_account").val() }, function(data) [
       $("#depositsubmit").append(data);
});

Open in new window

0
 
LVL 2

Author Closing Comment

by:axessJosh
ID: 38852335
Just had to make a minor adjustment after:

function(data) [

changed to:

function(data) {

just a note for future reference.  Thanks!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question