0

How can I change a parameter in a PHP file using jQuery? Here is my code, I do not understand why when I click the button the value for $val it doesn't change:

<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.10.2.min.js">
</script>
</head>

<body>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#test").hide();
    $.get( "jqueryphp.php", { name: "John", time: "2pm" } );
  });
});
</script>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p id="test">This is another paragraph.</p>
<button>Click me</button>
<?php 
if(isset($_GET['time'])) $val=$_GET['time'];
else $val='parametru ne transmis 0';
echo "<br>".$val."</br>"
?>

</body>
</html>
6
  • Why don't you think it changes? As far as I can see, you're not doing anything with the returned data. Commented Jan 5, 2014 at 0:51
  • is this page jqueryphp.php? Because if it is, this is not AJAX. Commented Jan 5, 2014 at 0:52
  • You should not have your PHP on the same page as your JavaScript when using AJAX. Commented Jan 5, 2014 at 0:53
  • is my mistake, is just 1 variable :$val...even so it dosent't work Commented Jan 5, 2014 at 0:54
  • 1
    jQuery can not interact with php variables. jQuery only manipulates the html data on your browser, you can send post/get requests to any php file to do something with your parameters. Commented Jan 5, 2014 at 0:55

2 Answers 2

2

When you load a page in your browser, the browser makes an HTTP request to the server, gets a response and renders it.

When you use Ajax, the browser makes an HTTP request to the server, gets a response, and makes it available to JavaScript.

It does not modify the current page automatically. That page has already been received in the previous response.

You must write JavaScript to use the data it gets in the response (with your current approach, this is done by passing a function as the third argument to $.get to manipulate the DOM of the page.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem seems to be here:

if(isset($_GET['time'])) $val0=$_GET['time'];
else $val='parametru ne transmis 0';
echo "<br>".$val."</br>"

What is $val0 versus $val? Shouldn’t it all just be $val? Also, the last line echo makes no sense. There is no ; at the end of the line. And what is this about "<br>".$val."</br>" <br /> is simply a line break. Not an element you have to open & close. Do you want it to have a line break before & after? I am assuming so. Here is my cleanup of that. Should work:

if (array_key_exists('time', $_GET) && !empty(trim($_GET['time']))) {
  $val = $_GET['time'];
}
else {
  $val = 'parametru ne transmis 0';
}
echo '<br />' . $val . '<br />';

I also changed the if(isset($_GET['time'])) to something more robust. Because even if $_GET['time'] is set, it doesn’t mean it has a value.

3 Comments

thx but it dosen't work, i expect after I click the button also my $val to change so the message "parametru ne transmis 0' to be change with "2pm"
after I changed with your code i get this error "Fatal error: Can't use function return value in write context in C:\xampp\htdocs\e-learning\jqueryphp.php on line 23".......$GET['time'] it is not chnge the value of $val after the button is pushed? so the new message "2pm" to appear on the screen
even in html file it dosen't work, do you have a suggestion please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.