Asked  6 Months ago    Answers:  5   Viewed   21 times

Well here's what I got, basically a button is clicked and the following code is executed:

    Readthis = "MonsterRequest.php?id=<?php echo $_REQUEST['id']; ?>&Mon=";
    TestVar = TestVar.replace(/s/g, "");
    Readthis = Readthis + htmlencode(TestVar);
    $('#CenterPiece').load(Readthis); 

Everything seems to work as intended except for when passed to Monsterequest.php, the post isn't being passed onto it's self and the page basically reloads it's self as it was being passed onto the main parent...(I was unsure if it was but monsterrequest.php seems to work perfectly fine on it's own however when loaded it doesn't work as intended at all and I'm completely stumped. I basically want to post data without having the page reloaded.

Monsterrequest.php: http://pastebin.com/fiFjjBZg

 Answers

99

Are you familiar with AJAX? Forgive me if you know this already, but in case you don't:

Ajax posts data to an external php file, which processes the data it receives, and sends back an answer. It looks like this:

FILE #1:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#Sel').change(function() {
                    var opt = $(this).val();
                    var someelse = 'Hello';
                    var more_stuff = 'Goodbye';
                    $.ajax({
                        type: "POST",
                        url: "receiving_file.php",
                        data: 'selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,
                        success:function(data){
                            alert('This was sent back: ' + data);
                        }
                    });
                });
            });
        </script>
    </head>
<body>

<select id = "Sel">
    <option value ="Song1">default value</option>
    <option value ="Song2">Break on through</option>
    <option value ="Song3">Time</option>
    <option value ="Song4">Money</option>
    <option value="Song5">Saucerful of Secrets</option>
</select>

FILE #2: receiving_file.php

<?php
    $recd = $_POST['selected_opt'];
    echo 'You chose: ' . $recd;
Tuesday, June 1, 2021
 
elias
answered 6 Months ago
76

Due to security issues forms are not supported and not recommended within emails. Most of email clients will warn the user of a risk or simply will disable it and it will not work, like in your case.

The best practice for enabling unsubscribing is by using a link. You can pass any requierd parameter for unsubscribing on the link, for example http://www.example.com/unsubscribe.php?usermail=mail@gmail.com.

Saturday, May 29, 2021
 
Aamir
answered 7 Months ago
45

First - Do make sure that the database table structure does not have those fields (createdDate and updatedDate) as NOT NULL.

Second - InputFilter has more like - strict behavior. If you want any element to be required or not, then you have to specify that explicitly.

Try setting the Filter for 'createdDate' and 'updatedDate' as 'required' => false,

$this->add(array(
    'name' => 'createdDate',
    'required' => false,
    ....
));

$this->add(array(
    'name' => 'updatedDate',
    'required' => false,
    ....
));

At least this way, those messages wont be added in the errors() array.

Saturday, May 29, 2021
 
insomiac
answered 7 Months ago
88

There is a JQuery plug-in that accomplishes pretty much what you're trying to do: https://github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js.

After including JQuery and the jquery.redirect.min.js plug-in, you can simply do something like this:

$().redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'});

Use the following code on newer JQuery versions instead:

$.redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'});

Hope this helps!

Saturday, June 12, 2021
 
relyt
answered 6 Months ago
23

EDIT 2018

Yeah, I am still alive. You can use the fetch API instead of jQuery. It is widely supported except (guess who?...) IE 11 and below but there is a polyfill for that. Enjoy modern coding.

Support for fetch API

OLD ANSWER

You will have to use AJAX.

Javascript alone cannot reach a php script. You will have to make a request, pass the variable to PHP, evaluate it and return a result. If you'are using jQuery sending an ajax request is fairly simple:

$.ajax({
    data: 'orderid=' + your_order_id,
    url: 'url_where_php_is_located.php',
    method: 'POST', // or GET
    success: function(msg) {
        alert(msg);
    }
});

and your php script should get the order id like:

echo $_POST['orderid'];

The output will return as a string to the success function.

EDIT

You can also use the shorthand functions:

$.get('target_url', { key: 'value1', key2: 'value2' }).done(function(data) {
    alert(data);
});

// or eventually $.post instead of $.get
Saturday, June 19, 2021
 
Xatoo
answered 6 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share