0

Currently i am trying to add some jQuery in to my php project i have never worked with jQuery before so i am kind of stuck i have this function that dynamically adds text fields the input fields have some php functions. My question is in what way can i change this so it would work with jQuery. All i can figure out is that the commas are creating the error i tried to rearrange them and even remove them but that did not fix the problem.

<script type='text/javascript'>

$(document).ready(function()
{
    $('#button').click(function()
    {
       $('body').append('<pre><input type="text" name="content_prt1[]" size="50" value="' . str_replace($simbols, "",$part1).'"> 
                 <input type="text" name="content_prt2[]" size="50" value="' .str_replace($simbols, "",$part2).'"></pre>''); 
    });
});

</script>

Is this even the right way of executing this code or am i way off here?

4
  • You can't mix in your php like that..... Commented Jun 10, 2013 at 7:49
  • you cant have php in a script but you can have script in your php Commented Jun 10, 2013 at 7:51
  • can you show me what is the right way of combining php with jQuery in this sort of situation ? for replying Commented Jun 10, 2013 at 7:52
  • Well why are you using Jquery in the first place, to append something that can just be echoed with php?? and to answer...best practice is to build js/DOM with JSON as the data source Commented Jun 10, 2013 at 7:56

4 Answers 4

3

Echoing php into a script is considered bad practice...but if you really can't avoid it, and think of a better way to do it...then here you go....

It was because you have to open the php tags and echo those str_replaces...

 $(document).ready(function(){

   $('#button').click(function()
   {
    $('body').append('<pre><input type="text" name="content_prt1[]" size="50" value="<? echo str_replace($simbols, "",$part1);?>"><input type="text" name="content_prt2[]" size="50" value="<? echo str_replace($simbols, "",$part2);?>"></pre>'); 
   });
 });

Sign up to request clarification or add additional context in comments.

5 Comments

I believe you can't line break in the middle of a JS string like that, not without escaping it.
tnx for the reply what would be the right way of adding the php to j query can you point me to a page that can explain this ? I do like to be the guy that dose everything by the books :)
The correct way would be to do an ajax call with Jquery, and receive JSON data, then you parse that data, and build your elements...
Or another way is to store some data in the DOM, in the new data attributes of HTML5, and use them in your Jquery however you please...that way you don't have to hit the server needlessly, the data is already present from the first call.....its a little hard to understand if its the first time hearing any of this..but once you read a few tutorials and let it sink in, its easy...
tnx for the advice i will definitely look this up :)
2

For more clarity u can always indent your code -

$(document).ready(function()
{
    $('#button').click(function()
    {
       $('body').append('<pre>
       <input type="text" name="content_prt1[]" size="50" value="' + "<?php echo str_replace($simbols, \"\",$part1) ?>"+'"> 
       <input type="text" name="content_prt2[]" size="50" value="' + "<?php echo str_replace($simbols, \"\",$part2) ?>"+'">
       </pre>''); 
    });
});

4 Comments

@KyleK thx :) but I am still interested what your About Me is ..hard to decipher it :)
i am trying your example but i am getting a an error in this line $('body').append('<pre> near the (' part
You can't read binary??? lol....j/k...neither can I....copy it into a binary decoder to see what it says :)
@user2362103 yeah quotes not properly escaped ..give a try once more and it will
1

You must output your PHP code by <?php echo $variable; ?>. Try this:

$('body').append('<pre><input type="text" name="content_prt1[]" size="50" value="<?php echo str_replace($simbols, "",$part1); ?>"><input type="text" name="content_prt2[]" size="50" value="<?php echo str_replace($simbols, "",$part2); ?>"></pre>'); 

1 Comment

your solution worked great i see where i have went wrong. I still have a lot to learn tnx for your help and tnx everyone for fast replies i love this community :)
1

when need to add something from php out should be written as

value="<?php= str_replace($simbols, "",$part1) ?>

Comments

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.