1

I have this button by ID of genPDF, which if clicked has this function:

$('#genPDF').click(function () {

    var str = "headingText=" + jQuery("#headingText").val();

    $.ajax({
        url: 'indexpdf.php',
        data: str,
        dataType: "json",
        type: 'post',
        success: function () {
            console.log("Success!");
        }
    });
})

It's supposed to take the text from input "headingText" and send to the php file indexpdf.php.

Then here's my indexpdf.php:

<?
$headingText = trim(isset($_POST['headingText']) ? $_POST['headingText'] : '');

$initialpdf = file_get_contents('file_html.php');

$initialpdf = str_replace(array(
        '%headingText%'
    ), array (
        $headingText,
    ), $initialpdf);

file_put_contents($fp, $initialpdf);
?>

This file is supposed to decalre the headingtext variable from the previous page, and replace each "%headingText%" in file_html.php, then save this html/php file as "file_html2.php"

The problems are, I don't think I'm saving the finalized php file right, and for some reason, while passing the "str" into the ajax php call, it doesn't succeed. If I get rid of the line, "data: str,", it succeeds.

How can I pass in these string variables, replace them in the php file, and save that file as a different file succesfully? What's wrong here?

2
  • What is the error ? "...while passing the "str" into the ajax php call, it doesn't succeed.." What Problem you are facing ? Commented Apr 11, 2016 at 19:05
  • @NanaPartykar it never makes it to the console.log call, and there are no errors mentioned Commented Apr 11, 2016 at 19:07

4 Answers 4

2

An Alternative Way

<script>
$('#genPDF').click(function () {
  var str = $("#headingText").val();
  $.ajax({url:"indexpdf.php?headingText="+str,cache:false,success:function(result){
        console.log("Success!");
  }});
})
</script>

Use _GET

<?
$headingText = trim(isset($_GET['headingText']) ? $_GET['headingText'] : '');

$initialpdf = file_get_contents('file_html.php');

$initialpdf = str_replace(array(
        '%headingText%'
    ), array (
        $headingText,
    ), $initialpdf);

$initialpdf->saveHTMLFile("file_html2.php");
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your approach and the only thing wrong is that "file_html2.php" is neither being created, nor gets contents placed inside of it. See my revised code..
1

You want to pass an object to data. For this, replace:

data: str

with:

data: { headingText: jQuery("#headingText").val() },

This should pass the value correctly

1 Comment

This doesn't work. the ajax call doesn't get to the console call.
1

First Part You are forcing ajax request to json type and trying to post data as string thats why its not working, try posting your headingText in JSON format i.e.

data: {headingText: jQuery("#headingText").val()}

this will make sure you receive data in PHP

Second Part

After making all the changes you can use alternate function to file_get_contents to store file once manipulation is done i.e. file_put_contents Use it like

$initialpdf = file_get_contents('file_html.php');

$initialpdf = str_replace(array(
        '%headingText%'
    ), array (
        $headingText,
    ), $initialpdf);

file_put_contents("file_html2.php", $initialpdf);

this should address your both issues.

2 Comments

I tried your approach and the only thing wrong is that "file_html2.php" is neither being created, nor gets contents placed inside of it.
what does the error logs say? can you try these steps $handle = fopen("file_html2.php", 'w'); fwrite($handle, $initialpdf); fclose($handle);
-1

Try sending it as a json object instead of a string.

str = {headingText:'blabla'}

1 Comment

This does not work because you have made a mistake in your code. You need to set method : post Not type:post. No need to send it in url which is not considered safe unless data is not sensitive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.