1

I have spent all morning trying to figure out how I can call a PHP script from my JavaScript function and then return a value to output. I am not sure whether I can do this with just JavaScript or whether I have to include some Ajax aswell?

Here is the main page

<script>
 $("document").ready(function (){ 
 $("select").change(function(){
 var currentValue = parseInt($(this).find("option:selected").val());
 //Call Script script.php here
 $("#output").html(result);
 })
 }); 
</script> 
</head>
<body>
 <select name="select">
 <option value='1' >1</option>
 <option value='2'>2</option>
 <option value='3'>3</option>
 </select>
 <div id="output"><!---OUTPUT HERE----></div>

And here is the script I want to call (I have simplified it all down to make it easier to understand)

<?php
 $result = $currentValue + 1;
?>

Basically I want it to read in the currentValue variable and output the result variable back to the webpage, to be outputted again.

Teun Zengerink
4,4135 gold badges33 silver badges32 bronze badges
asked Jul 21, 2012 at 11:38
4
  • 1
    Have a look at jQuery.get() Commented Jul 21, 2012 at 11:47
  • 2
    What's the use of increasing a value like that? You need to store the value using for example like MySQL! Commented Jul 21, 2012 at 11:49
  • 1
    What do you mean by increasing it like that, the php script i have written is very long, too long to post here that involves mysql and all that, i just made it simple to show the fact, that i need to get the currentValue into the php script and get the result value out and back to the main page Commented Jul 21, 2012 at 11:51
  • look at this example I wrote .. you can download the files devpazzi.com/how-to-get-data-from-php-script-with-ajax-jquery Commented May 8, 2015 at 4:54

2 Answers 2

3
// Javascript:
$.ajax({
 type: "GET",
 url: "example.com/script.php?currentValue="+currentValue ,
 dataType: "json",
 statusCode: {
 200: function (result)
 {
 $("#output").html(result.value);
 }
 }
 });
// PHP 
<?php 
$result = $_GET["currentValue"] + 1;
echo json_encode(array("value" => $result));
 ?>
answered Jul 21, 2012 at 11:53

7 Comments

Hi, i am trying your code now, but i get an error on the last line echo json_encode(array("value" => $result);
ok, i just tried it, but whenever i select another option from the dropdown, nothing appears? Thanks for your help so far
Do you replaced "//Call Script script.php here" with the $.ajax ?
yes i replaced it exactly like that, but still it doesnt display anything
Edited my answer, updated line $("#output").html(result.value)
|
1
$.post('<url>', {currentValue : currentValue}, function(){}, 'json');

< url> example ajax/phpScript

Here ajax is controller and phpScript is the method in that controller

public function phpScript()
{
 $phpScript = $_POST['phpScript'];
//add your code 
}
answered Jul 21, 2012 at 11:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.