SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding onclick to php echo line

    Hi all

    I am trying to use...

    Code HTML4Strict:
    onClick="confirm('Complete Delete?')"

    for exmaple below in a line of PHP...

    Code PHP:
    echo '<td><form action="salesdelete.php" method="POST"><input type="submit" name="submit" value="Delete" onClick="confirm('Complete Sale?')"><input type="hidden" name="invoice_no" value='.$row['invoice_no'].'></form>';

    But keep recieving the following error message when trying to open the page in a web browser...

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\trhayes\salesdisplay.php on line 139
    Can anoyone highlight how the syntax should be constructed??

    Many thanks

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Hi Chris,

    Have a good read through the PHP strings documentation (at least the "Single quoted" section).

    Dealing with your issue in particular, note the following taken from that page

    To specify a literal single quote, escape it with a backslash (\).

    With that, your code would become:

    echo '<td><form action="salesdelete.php" method="POST"><input type="submit" name="submit" value="Delete" onClick="confirm(\'Complete Sale?\')"><input type="hidden" name="invoice_no" value='.$row['invoice_no'].'></form>';

    Regards,
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Hi Chris,

    Have a good read through the PHP strings documentation (at least the "Single quoted" section).

    Dealing with your issue in particular, note the following taken from that page

    To specify a literal single quote, escape it with a backslash (\).

    With that, your code would become:

    echo '<td><form action="salesdelete.php" method="POST"><input type="submit" name="submit" value="Delete" onClick="confirm(\'Complete Sale?\')"><input type="hidden" name="invoice_no" value='.$row['invoice_no'].'></form>';

    Regards,
    Thanks for your reply Salathe

    This has fixed the issue with getting this confirmation box to appear, but pressing 'ok' or 'cancel' seams to delete regardless. My apologies. I have never encountered the onclick function before, hence the forum requirement.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chrisjwebb1978 View Post
    This has fixed the issue with getting this confirmation box to appear, but pressing 'ok' or 'cancel' seams to delete regardless. My apologies. I have never encountered the onclick function before, hence the forum requirement.
    That's okay, you only asked about fixing the PHP syntax issue. The confirmation not doing its job is a JavaScript issue. Within the onClick, you can return false to stop the default behaviour (submitting the delete) from happening. The confirm() will return either true or false depending on whether the cancel or ok button was clicked.

    You can simply do:

    …onClick="return confirm(…
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    That's okay, you only asked about fixing the PHP syntax issue. The confirmation not doing its job is a JavaScript issue. Within the onClick, you can return false to stop the default behaviour (submitting the delete) from happening. The confirm() will return either true or false depending on whether the cancel or ok button was clicked.

    You can simply do:

    …onClick="return confirm(…
    Awesome, I salute you!

    I have a related question regarding another onClick feature, but feel free to discard this scenario...

    I have a drop down select box that populates using php from a stock table. I also have a text box near this i would like to display the current available stock for whatever is selected....so same table. Below is the code...

    Drop down...

    Code PHP:
    <label for="item1_item" title="Please select the 1st item from the drop down list"><label/>
      <select name="item1_item">
      <?php
      /* connect to database */
      require 'connect.inc.php';
     
      /***
       ** CREATE QUERY TO EXTRACT AVAILABLE STOCK
       ** STORE IN VARIABLES FOR USE IN SELECT FIELD
       ***/
     
      /* drop down query for stock */
      $stock_query  = "SELECT `description`
                       FROM   `stock`
                       ORDER BY `description`";
      $stock_result = mysql_query($stock_query);
      $numrows      = mysql_num_rows($stock_result);
     
      /* PHP SELECT MENU
            1 - create "Please select" default category
            2 - create for loop to cycle through stock
            3 - assign a variables for stock descriptions...
            4 - ...display stock descriptions
      */
      echo '<option value="">Please select an item...</option>';
      for($i=0; $i < $numrows; $i++)
      {
      $stock_name = mysql_result($stock_result,$i,0);
      echo'<option value="'.htmlspecialchars($stock_name).'">'.htmlspecialchars($stock_name).'</option>';
      }
      ?>
      </select>

    Disabled text box directly after i would like to have populated with the level of stock for whichever items was selected.

    Code HTML4Strict:
    <label for="item1_availablequantity" title="Available Quantity">Available Quantity: </label>
      <input id="item1_availablequantity" name="item1_availablequantity" type="text" maxlength="2" size="2" disabled="disabled" />

    How would i construct this?

    Thanks for any advice you are able to give!

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Note that a confirm dialog actually has three options in most modern browsers since it is intended primarily for debugging use. As well as being able to return true or false depending on which button is pressed some browsers also provide an option for disabling all subsequent dialogs so that the next time a confirm is called it will just return true without displaying anything at all. In other browsers the third option is to turn off JavaScript for the page in which case nothing gets returned at all as the page now has JavaScript turned off until you close the browser.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •