Passing Variables References to PHP from Javascript via AJAX -


i have idea "using", or "referencing" php variables in javascript. apply webpage send email. simplified example shown below. note: called via ajax, not case trying call php variable script has been executed.

the javascript include "$midsection" string in body of email sent, , send entire body php script. php script store string, create , assign value $fmidsection, , send body string in email. if works, resulting email include main body sent client side, inserted "midsection" in middle of email (perhaps, depending on person's name , info stored in database).

it seems me should work, given understanding of php. however, seems me open window attack similar sql injection (where' perhaps, can trick script assign different value $midsection, example). has taken approach, , if so, can validate whether work, , open security holes?

thank you.

edit: application mailing list, not contact form. have admin panel allows me send emails mailing list, , thinking way include variables php in similar way on php script, putting $var in string itself. understand how passing variables js php works, want js reference php variable, essentially. not using validation purposes, using easy way insert information, rather doing string parsing manually. variable created , stored server side on script have created.

also, javascript performing ajax call on php script. therefore, javascript executed first. i'm sending email template php, php loop through email list , add information dynamically, such first name, last name, etc. instead of doing string processing, i'm thinking of sending "hello, $firstname $lastname....." essentially, in hopes php script insert variable information.

from comments above can see you're trying do, won't work.

consider following code:

$(document).ready(function() {     $.ajax({         url: 'ajax.php',         data: {'name' : 'andy'},         method: "post",      }).done(function (data) {         console.log(data);     }); }); 

this ajax.php:

<?php echo $_post['name']; ?> 

all you're doing in javascript making post request ajax.php. it's able give output "andy" in console because you're passing data string - not reference anything. far, simple.

now imagine if change data: in jquery following:

data: {'name' : '$var'} 

in console string "$var".

even if had in ajax.php:

<?php $var = 'foo'; echo $_post['name']; ?> 

you never output "foo".

this because php , javascript separate. if pass $var, it's going treat string. there's no way of asking javascript mean php variable or reference. have pass data itself.

in case of application, you'd typically pass in ajax request php can refer (like primary key id particular record). php generate of required content , send browser. if need things template, str_replace friend.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -