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

 

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.