Php javascript combination

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
rahul09
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Jan 27, 2017 4:51 am

Fri Jan 27, 2017 5:37 am

I need to insert a combination of php and javascript variables.
This is the code

Code: Select all

					<?php	
						echo '<div id="output2"></div>
						<button id="sel" onclick="select()">Select</button>';
						echo '<script>  
						var create, cc="",mm;
						function select() 
						{
							create=document.getElementById("output2").textContent;
							qq=create;
							creater(qq);
						}	
						echo '<script>function creater(qq) { cc=qq;  mm ="'.$mm="'<script>alert(cc)</script>'".'";  }</script>';
						echo $mm; // need javascript value here
 						?>
I need to assign JavaScript variable value(cc) to php variable $mm. Is this possible? How can I achieve this?

element121
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Mon Jan 30, 2017 6:34 am

You would need to use an AJAX call to POST the value of the JS variable to PHP code.

boxfly
New php-forum User
New php-forum User
Posts: 9
Joined: Mon Mar 06, 2017 5:17 am

Tue Mar 07, 2017 1:41 am

Yes and if you want to use aJax call back, here is an example:

Code: Select all

function myFunction() {
        var values = $("form#MyForm").serialize(); // Recover variables from FORM to post them through aJax call
        $.ajax({
            url: "url-to-php-file",
            type: "POST",
            data: values,
            success: function(data) {
               // Do some stuff, no error
               alert(data); //Will display the content of ajax php file
            },
            error: function(exception) { console.log(exception); }
        });
}

Post Reply