1

I have the following question in my PHP survey form: enter image description here

As can be seen in the image, when user select "By actor", a textbox appears(I uses jQuery auto-completion for this). Then, what I need is when user click the button "Movies by this actor", a list of movies by this actor (in this case: Tom Hanks), will be shown in a new window.

In order to get movies by the actor that user has inserted in the textbox, first, I have to be able to access selectedVal in child window. Then, I have to run a database query to get all movies by selectedVal(actor name in the textbox).

This is my code:

<html>
<body>
<div id="m_scents2" class="field2" style="display:none;">
 <input type="textbox" name= "tag" id="tags" placeholder="Enter an actor/actress name here" />
 <input type="button" value="Movies by this actor" id="btnRight" />
</div>

<script type="text/javascript">
var selectedVal;

$(document).ready(function () {
// ...
//..
if ($(this).val() == "byActor"){
      $("#tags").focus();
      $("#tags").autocomplete({
           source: "actorsauto.php",
           minLength: 2,
           focus: function( event, ui ){
               event.preventDefault(); 
               return false;
            },
           select: function (event, ui){ 
                window.selectedVal = ui.item.value;
           }
       });  
 });         

$('#btnRight').on('click', function (e) {
           popupCenter("movieByactor.php","_blank","400","400");
 });                   
</script>
</body>
</html>

and this is child window (movieByactor.php):

<script type="text/javascript">
  var selectedVal = parent.window.opener.selectedVal; 

 $.ajax({
         url: 'childfilm.php',  //childfilm.php IS A PHP FILE WHERE I RUN SQL QUERY TO GET MOVIES BY THE ACTOR ...
         datatype: "json",
         data:{q:selectedVal},
         success: function(response) {
                     alert(JSON.stringify(response));      
                   }
        });
</script>

My question:

Now my question is how can I use php inside javascript? I know that php is server side while javascript is client. I read lots of similar questions and I used ajax, but with the above code, when I click on the button, a new window appears but it is empty.

I really appreciate if someone can help me.

Thanks in advance,

5
  • Put childfilm.php in quotes, you're using it as if it's a variable at the moment, not a string literal. It should look like url: "childfilm.php", Commented Nov 11, 2014 at 15:16
  • put quotes around it like so.. "childfilm.php" Commented Nov 11, 2014 at 15:16
  • set the url complete like this "yourdomain/childfilm.php" Commented Nov 11, 2014 at 15:17
  • Ooooops, I forgot!! Thanks:) Commented Nov 11, 2014 at 15:20
  • You added the quotes url: 'childfilm.php', in an edit. You should always make note that an edit has been made to that effect. People giving answers may get downvoted because of it, when people will see your question and say to themselves: "There's quotes in there, why the answer?". Commented Nov 11, 2014 at 15:20

2 Answers 2

2

childfilm.php should be in quotes

<script type="text/javascript">
  var selectedVal = parent.window.opener.selectedVal; 

 $.ajax({
         url: 'childfilm.php',  //childfilm.php IS A PHP FILE WHERE I RUN SQL QUERY TO GET MOVIES BY THE ACTOR ...
         datatype: "json",
         data:{q:selectedVal},
         success: function(response) {
                     alert(JSON.stringify(response));      
                   }
        });
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

If you place your url between quotes the problem is fixed: url: 'childfilm.php'. Without the quotes javascript thinks it's an object (which will return undefined).

Comments

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.