-2

This is the PHP function, it is messy right now (redundant lines) that I will clean up after. I am stuck right now, really trying to keep the whole process to one page:

<?php function signpetitiion() {
$API_KEY = 'MYAPIKEY';
$REQUEST_URL = 'https://api.change.org/v1/petitions/get_id';
$PETITION_URL = 'https://www.change.org/p/URLTOMYPETITION';
$parameters = array(
  'api_key' => $API_KEY,
  'petition_url' => $PETITION_URL

}


?>

So this doesn't work because it is browser side:

<form onsubmit="sign_petitiion()">

How do I get this function to run with a button click? Should I convert to a JS script? if so how?

Or call with AJAX? if so how?

if(isset($_POST['go'])){

    <input type="submit" name="go" method="post">

Question closed as I was typing my answer

I know this question (in different ways) has been asked before, I had read through them but the answers were either too complicated for me (NEWB) or just a comment saying you can't.

Any ways I have found my answer,

I had to change these 2 lines

function sign_petitiion() {

to

     if(isset($_POST['go'])){

and then

<form onsubmit="sign_petitiion()">

to

    <form method="Post" class="sign" action="mypage.php">
2
  • 1
    It doesn't work that way. You can't call a PHP function from Javascript directly. You can either submit to a particular PHP page and refresh the page, or do an Ajax call to a PHP page. But you can't call a PHP function. You'd have to give your submit or Ajax call a parameter to tell what function you want the PHP page to run. Commented Dec 10, 2015 at 23:08
  • So I have been doing a lot of reading and it seems I should use Ajx? I am just a newb working on a personal webpage so its a bit over my head but Ill keep at it. Or, would it be better to convert this php function to a js script? and if so, how would I go about that? Thank you for your time. Commented Dec 12, 2015 at 16:00

1 Answer 1

1
<!-- HTML-->
<a href='index.php?run=true'>Execute it </a>

//*.php
if($_Get['true']){
    signpetitiion();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Some questions sound like: I want to drive fast around that and than up to full speed and again right and fullspeed again, but, i dont where to put the keys in. Can you help me making that key? :)
Thanx for quick reply, I have tried for 2 days now, but just can't get the function to run I have tried several ways (changed to post as the page is posting) Just no luck, but I will keep trying. eg !code~ <form method="Post" class="sign" action="pet3.php?run=true"> <a href='pet3.php?run=true'>Execute it </a> <?php // if($_get['true']) // sign_petitiion(); if($_Post['true']){ sign_petitiion(); } ?>
original question edited' Thank you @Kisaragi for pointing me in right direction

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.