This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I was going to run krimaction1, a PH function when the JavaScript runs, but I've not had any luck solving this and I'm not really good at JS. How can I run that PHP function in the JavaScript?
Here is the code I've tried so far
<?php
function krimaction1() {
echo"IT IS ALIVE!";
}
?>
<script>
$(function(){
$('#krim_1').click(function(){
alert(this.id);
var myTD = this.id.split('_')[1];
var newFrm = '<form id="myNewForm1"><input name="newdata" value="' +myTD+ '" /></form>';
$('body').append(newFrm);
$('#myNewForm').submit();
});
});
</script>
I've tried another method with this as my JS:
function myFunction() {
document.getElementById("ThisIsTheValue").value = "Johnny Bravo";
document.getElementById("SubmitForm").click();
}
but that code doesn't work either for me, as when I try to set the value with the JS and click it it doesn't do anything and I can't really see what's wrong.
It seems the action of myNewForm is the php page itself, so you can add
<?PHP
function krimaction1() {
echo "IT IS ALIVE!";
}
if (isset($_GET["newdata"])) { krimaction1(); }
?>
to the page to have it execute when the form is submitted
I use $_GET here since that is the default for the form you have created.
If you do NOT want to reload the page to see IT IS ALIVE, you need to AJAX the result
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
My aim is for a user to click a button within a row to delete that row within the database.
The button is declared as so:
<td><button class="btn btn-default" onclick="removeStudent()" button id= <?php echo $productDetails["studentID"]; ?> type="button">Delete</button></td>
The remove function is as follows:
<script>
function removeStudent() {
$id = window.event.target.id.toString();
$sql = "DELETE FROM tableStudent WHERE studentID= '$id'" ;
if (mysqli_query($connection, $sql)) {
echo "Record deleted";
} else {
echo "Error deleting";
}
}
</script>
The webpage is connected to the database, and variable names are correct and working within other functions. Just delete function not working. Any help would be much appreciated.
It looks like you are mixing javascript and PHP here. I'm not sure you understand how PHP works. Any PHP code on your page is run before the page is ever sent to the user, so you can't put PHP stuff inside javascript function (well you can, but it will be run and turned into plaintext before the javascript is ever even read by the user's browser.) So your line where you go $id = window.event.target.id.toString(); won't work, since you are trying to set a PHP variable using javascript. Actually none of that code will do anything, since its not inside a <?php block. It will just cause errors in the browser console because it is invalid javascript code.
You need to make your javascript function make an ajax call to another PHP page, which actually does the delete
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have an idea how to pass PHP value into a jQuery variable, I have done it in the past and it worked. But I tried the below code but for some reason it not working. I dont know where the problem lies.
I am trying to hide a custom virtuemart drop down cart I designed when the cart is empty.
jQuery(document).ready(function ($) {
var cart = "<?php if ($data->totalProduct) echo $data->cart_show; ?>"
jQuery('.btn-cart1').hover(if (cart === "") {
jQuery(this).find('.dropdown').hide();
}
else {
jQuery(this).find('.dropdown').hide();
jQuery('.btn-cart1').hover(
function() { jQuery(this).find('.dropdown').stop().show(500)},
function() { jQuery(this).find('.dropdown').stop().hide(500)})
}
});
PHP is server side scripting language and is executed well before your javascript executes on web browser.
The correct execution of your code depends on the values of $data->totalProduct & $data->cart_show after the PHP has been executed on your server side and the values are places in your javascript.
It is not a good programming practice and I would strongly suggest you to use AJAX instead to access the values of PHP variables.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I am struggling to get the following code to work. I want to take input from a javascript prompt, and output it using a php echo. When I try this I get an echo with the random numbers "12313" for no apparent reason. This is my code:
echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>'
, '<script type="text/javascript">'
, 'var code = prompt("Enter verification code", "");'
, 'var getcode = code;'
, '$.post("wp-members-register.php", { code: getcode }); </script>';
$buffer_data['code'] = $_POST['code'];
echo $buffer_data['code'];
I am very new to php so please bear with me. Perhaps I am not correctly posting the 'code' variable?
EDIT: Maybe somebody can show me a better way of inputting a text string and getting it in the php code to follow? Note I am working with the wp-members wordpress plugin in the wp-members-register.php file.
Outputting javascript using PHP doesn't make any sense as javascript is a client side scripting which is executed after loading the DOM elements. PHP code is triggered when the request is sent from the browser to the server.
PHP is called before the JavaScript and cannot be called after.
You have to do everything you have to do in PHP before you use any JavaScript.
On thing you can do is use AJAX.
<?php
//php code here before the javascript
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var code = prompt("Enter verification code", "");
$.get( "wp-members-register.php?code=code", function( data ) {
document.write("Data Loaded: " + data);
});
</script>
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I have a code in php where onclick i will call a javascript. but i dont know how to use the ajax and pass the value to php and return it.
<script>
function getURL(e)
{
//ajax code that will check.php
}
</script>
body: main.php (current page) when the user clicked on the link. it will alert correct.
<?php $url = "www.google.com"; ?>
click me
a php that ajax will call.. check.php
<?php
$html = file_get_html($url);
foreach($html->find('iframe') as $e)
echo $e->src;
return "correct" //idk if it's correct to return. should it be echo? ?>
i need to return the result.
May a ask for some sample basic code to call of ajax to php using this? and it will alert the "correct" word. or anything that the php will return. thanks :)
May a ask for some sample basic code to call of ajax to php using this?
and it will alert the "correct" word. or anything that the php will return
I think you don't to use ajax for this kind of alert:
HTML:
click me
<div id="result"></div>
JavaScript Code:
function getUrl( url ){
var result = document.getElementById('result');
// Just a Simple validation
var regex = /www\.google\.com/;
if( regex.test(url) ){
result.innerHTML = "Correct";
}
else{
result.innerHTML = "Error";
}
}
Demo:
Check out the working example here: http://jsfiddle.net/jogesh_pi/6UGFT/
Something like:
<script>
function getURL(e){
//ajax code that will check.php
$.get('check.php',function(data){
alert(data);
});
}
</script>
might do the trick
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I am trying to make a confirm box which will desire which php code will be executed.
Heres my code:
<script type="text/javascript">
var answer = confirm('Are you sure?');
if(answer==true)
{
<?php $confirmation = 1; ?>
}
else
{
<?php define("CONFIRMATION", 1, true); ?>
}
alert('<?php echo $confirmation; ?>')
alert('<?php echo defined("CONFIRMATION"); ?>')
</script>
The problem is , even if i click YES, $confirmation and boolean from defined() function returns 1.
Whatever I click, (cancel or ok) one of them should be 0 (I've already declared $confirmation before)
But both of codes at if and else blocks are used!
Normally it works like this
You fundamentally misunderstand what PHP is doing.
PHP is evaluated on the server before the page is sent to your browser. By the time the browser sees it and executes the javascript, all the PHP is gone.
Use your browser's "view source" on the browser window with this code in it. You'll see it looks like this:
<script type="text/javascript">
var answer = confirm('Are you sure?');
if(answer==true)
{
}
else
{
}
alert('1')
alert('1')
</script>
You either need to implement what you want to do in javascript to run on the browser, or you need to send a new request to the server and get a new page back (either directly or indirectly) with your response.
That will never work because PHP is processed before the output is sent to the browser. If you really need to modify something in PHP then try using an AJAX call.
http://ajaxpatterns.org/XMLHttpRequest_Call
Or try using jQuery's $.ajax(); function. Start by looking here.
Here is a quick example:
<script type="text/javascript">
var answer = confirm('Are you sure?');
$.ajax({
type: 'GET',
url: '/path/to/script.php',
data: 'answer=' + answer,
success: function(response) {
alert(response);
}
});
</script>
Contents of script.php:
<?php
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
// AJAX request
$answer = $_GET['answer'];
// ...
}
You can't trigger a PHP code exection without a post/get request.
For your needs, you should choose between a form submisssion or load a page-link with parameters stuffed in the query string on confirmation.
P.S.
the query string parameters are the ones following the "?" in the format variable=value
for example:
index.php?answered=1
you will be then able to retrieve these vatiable/values using PHP $_POST, $_GET or $_REQUEST variables in a way like this:
if ($_REQUEST['answered'] == 1) { //confirmed
...
}
You are misunderstanding the order of what will happen here.
Firstly, PHP will output the javascript layer. Your if block will then look like this:
if (answer == true)
{
}
else
{
}
The javascript engine should then optimise that out and totally ignore it. Consider using AJAX if you need to get PHP to process something with an input from the javascript layer.
Normally it works like this
No, it never works like this. PHP is executed before the javascript so it will never work like this.
I think from what I see you would want something like
Your link
This will go to the current page with $_GET['confirmation'] set to "1".
php executed before javascript so you can't do this because
when you check it via javascript if else statement php is already executed so you can't do it
but however you can use ajax for it