Jump to content

pass PHP variable value to jquery function


felito

Recommended Posts

hi

 

i have this code:

 

$something ="text";


<script type="text/javascript">
$(document).ready(function() {
    $("#custom").submit(function() {
        var formdata = $("#custom").serialize();
        $.ajax({
            url: "dojo/insert.php",
            type: "post",
            dataType: "json",
            data: formdata,
            success: function(data) {
                switch (data.livre) {

                case 'valorLivre':
                    $("#msg").fadeTo(200, 0.1, function() {
                        $(this).html('Deve fazer a ponderação de competências!').fadeTo(900, 1);
                    });
                    break;
               
                default:
                    $("#msg").fadeTo(200, 0.1, function() {
                        $(this).html('Oferta colocada!').fadeTo(900, 1);
                        $.ajax({
                            url: "oferta.php",
                            type: "post",
                            dataType: "json",
                            data:{
                                name:<?php echo $something; ?>	 	   //problem here
                            },

                            success: function(data) {
                            	$('#total').load('oferta.php');
                                }
                        });
                    });
                    break;
                }
            }
        });
        return false;              
    });
});
</script>

 

when i run the page i get text is undefined.

 

What is wrong in this code ?

 

thanks

 

I find that passing a PHP variable to a jquery function via an argument seems to be the best method..

 

$something = "text";
<form onsubmit='handleForm("<?php echo $something; ?>");'>

 

then grab it like this..

 

function handleForm(something){
    document.write(something);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.