简体   繁体   中英

Jquery PHP echo dynamic data

I have a Jquery post script, which for reasons outside of my control has to be placed on a page by use of echo from PHP. Now the issue is that I need to pass information from a form on the page into the data to be posted.

Here is the working script at the moment:

echo '<script type="text/javascript">
$(".discuss_your_report").click( function() {
    $.post(\'/layout/standard/report-action.php\', {
        form: "discuss_your_report", website: "'.$_GET['website'].'", name: "'.$_GET['name'].'", email: "'.$_GET['email'].'", phone: ""},
        function(data){
            $(".message").html(data);
    $(".message").show("slow");
        }
    );
</script>';

In the above script the phone number is empty, this needs to be entered by the user in the page using a standard form input. I have tried many things that have not worked, this being the latest:

echo '<script type="text/javascript">
$(".discuss_your_report").click( function() {
    var phone = $(\'#phone\').val();
    $.post(\'/layout/standard/report-action.php\', {
        form: "discuss_your_report", website: "'.$_GET['website'].'", name: "'.$_GET['name'].'", email: "'.$_GET['email'].'", phone: "phone"},
        function(data){
            $(".message").html(data);
    $(".message").show("slow");
        }
    );
</script>';

For ease of readibility, here it is without the PHP code:

<script type="text/javascript">
$(".discuss_your_report").click( function() {
    var phone = $('#phone').val();
    $.post('/layout/standard/report-action.php', {
        form: "discuss_your_report", website: "www.example.com", name: "my name", email: "info@example.com", phone: "phone"},
    function(data){
    $(".message").html(data);
    $(".message").show("slow");
    }
);
</script>

But this just passes the text 'phone'. Does anyone know how I can get this to work, any feedback is appreciated. Thanks

try this instead:

<script type="text/javascript">
$(".discuss_your_report").click( function() {
    var phone = $('#phone').val();
    $.post('/layout/standard/report-action.php', {
        form: "discuss_your_report", 
        website: "www.example.com", 
        name: "my name", 
        email: "info@example.com", 
        phone: phone},
    function(data){
    $(".message").html(data);
    $(".message").show("slow");
    }
);
</script>

"phone" - this is text

phone - is variable

have changed var phone to phone_val, have assigned it with phone: phone_val without quote

echo '<script type="text/javascript">
$(".discuss_your_report").click( function() {
    var phone_val = $(\'#phone\').val();
    $.post(\'/layout/standard/report-action.php\', {
        form: "discuss_your_report", website: "'.$_GET['website'].'", name: "'.$_GET['name'].'", email: "'.$_GET['email'].'", phone: phone_val},
        function(data){
            $(".message").html(data);
    $(".message").show("slow");
        }
    );
</script>';

Try using a dynamic reference to the form element at the time of posting:

echo '<script type="text/javascript">
$(".discuss_your_report").click( function() {
    var phone = $(\'#phone\').val();
    $.post(\'/layout/standard/report-action.php\', {
        form: "discuss_your_report", website: "'.$_GET['website'].'", name: "'.$_GET['name'].'", email: "'.$_GET['email'].'", phone: document.forms.discuss_your_report.phone.value },
        function(data){
            $(".message").html(data);
    $(".message").show("slow");
        }
    );
</script>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM