Problems in PHP call

Asked

Viewed 170 times

0

I’m having a problem calling a show by PHP passing parameters.

Code:

<?
    print("<script language=javascript>
       alert(\" <<<  Dados Alterados com Sucesso!  >>>\");
       if($w_rec != "")
       {
           if (confirm(\"Deseja alterar as demais opções?\") == true)
           {
             location.replace(\"../sai_cada_peri/sai_frm_alte_peri.php?$w_rec+$w_cont\");
           }
           else{
            parent.location.replace(\"../sai_cada_peri/sai_alte_peri.php\");
            }
        }
        else{
            parent.location.replace(\"../sai_cada_peri/sai_alteperi.php\");
        }    
      </script>"); 
?>

In this excerpt from PHP he will check whether the $w_rec is empty, and if so and the user confirms it will call another program by passing these parameters. My problem is .. How to pass these parameters in this call?

The call I’m referring to would be on that line:

location.replace(\"../sai_cada_peri/sai_frm_alte_peri.php?$w_rec+$w_cont\");

Obs* The whole show is just PHP.

  • 1

    But... this is Javascript! Is the problem not only the lack of quotes and escape in if($w_rec != "")? Try if('$w_rec' != '')

  • Yes, in itself this part is a Javascript! But the variables I want to pass are in PHP Location.replace(".. /sai_cada_peri/sai_frm_alte_peri.php? $w_rec+$w_cont");

  • 2

    It is not clear what the problem is. If you are giving error in this JS, I think it is better to post the code generated by PHP (the output that the browser sees).

  • I suppose it can’t be the case with quotation marks. As @bfavaretto asked me to try, the program still doesn’t run!

  • 1

    But we need to see the output of this PHP to better understand! From a "display source code" in your browser and post the JS that your PHP spat.

  • Yes it was a quote problem. I’m sorry for the problem, it was giving error because I did not put the quotes in the parameter passage (".. /sai_cada_peri/sai_frm_alte_peri.php? '$w_rec'+'$w_cont'");

  • 5

    This question seems to be decontextualized because it is about a specific problem and will not help anyone else.

  • This question is being discussed at the goal: http://meta.pt.stackoverflow.com/q/1957/7210

Show 3 more comments

2 answers

7


Avoid mixing server and client languages in the same code

This practice makes it difficult to maintain and refreshes your code. Ideally, you separate the layers of code and pass the necessary information via an object or as parameters to a function, for example.

Practical example:

function sucesso(w_rec, w_cont){
    alert("<<<  Dados Alterados com Sucesso!  >>>");
    if(w_rec != ""){
        if(confirm("Deseja alterar as demais opções?") == true){
            location.replace("../sai_cada_peri/sai_frm_alte_peri.php?" + Number(w_rec+w_cont));
        }else{
            parent.location.replace("../sai_cada_peri/sai_alte_peri.php");
        }
    }else{
        parent.location.replace("../sai_cada_peri/sai_alteperi.php");
    }
}

And then you pass your PHP variables to the Javascript function:

sucesso(<?php echo $w_rec ?>, <?php echo $w_cont ?>);

2

I’m sorry I would try to help but since I don’t agree with the form of what is exposed I prefer to recommend the following:

first: Pass content to javascript variables... example: in html...

<script type="text/javascript">
    var w_rec = '<?php echo $w_rec ?>';
    var w_cont = '<?php echo $w_cont ?>';
</script>

then use the same code but in pure javascript... that any editor can help! I think it’s simpler, more intuitive and more manageable within the project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.